hermes-agent/tests/hermes_cli/test_provider_profile_identity.py
ethernet f3b1399211 fix: round-5 CI backlog after the 779-commit main merge
Merge fallout (my resolution errors, all caught by CI):
- hermes_cli/backup.py + gateway.py: `theirs` on those hunks re-imported clusters HEAD had
  already moved to backup_restore.py / kept in the facade. backup.py loses the 349-line
  duplicate (main's #110179 fix is ported into backup_restore._import_db_member); the
  systemd service-unit cluster returns to gateway.py (PM's _prepare_service_launcher /
  _pm_managed_node_dirs / _systemd_command have no home in main's extraction) with main's
  utf-8-sig read. gateway_service_unit.py is dropped.
- gateway/run.py: main's plugin-update chore is not profile-scoped (the housekeeping
  ordering test pins the scope/drain sequence).
- pyproject + 30 test files: `import yaml` -> `import hermes_yaml as yaml` (pm-clean has no
  pyyaml); gateway/config._bundled_platform_manifest_name reads through hermes_yaml.
- tests re-seamed onto pm-clean's shape: residency admission (installed_engine),
  supervisor child env (binary is a constructor argument), update import guard
  (update_cmd_deps is gone; our probe already scrubs PYTHONPATH — both #115032 invariants
  pass), shallow-count git responses (stash path asks `status --porcelain -z`); dropped
  tests for retired code (_run_node_bootstrap/_ensure_tui_node, Windows resume demotion).
- tests/tools/test_local_env_blocklist.py: restore the two helpers the suite-reduction
  commit dropped and the blocklist import.

Real fixes:
- pm: classify_uv_failure/ResolutionConflict move beside the uv runner (pm.environment,
  stdlib-only). pm.workspace imports tomllib at module level and cannot load on the 3.10
  bootstrap python that streams uv output in the Docker arm64 image.
- tools/browser_tool.warm_agent_browser_npx_cache: back as a permanent definition — it is on
  the frozen old-updater surface, and the revert-scheduled compat pointer does not count.
- hermes_cli/memory_setup: the dashboard's pip row uses pm.environments.
  running_from_selected_environment for installed vs restart_required.
- scripts/windows-build-deps.ps1: export DISTUTILS_USE_SDK/MSSdk so setuptools trusts the
  primed MSVC environment instead of asking vswhere (`env -i` test runner on win32-arm64
  compiling ruamel-yaml-clib); run_tests.sh forwards them.
- tests/pm/test_windows_build_deps.py: start the protocol test from a parent env without the
  toolchain variables the runner job already exports.
- tests/conftest.py scrubs HERMES_BUNDLED_PLUGINS (Nix-wrapped hermes on the dev host);
  tests/home_io_guard.py treats sys.path site-packages under the real home as the
  interpreter's installation (PM-activated developer shell).
- tests-js: four `curly` lint errors from main's new scripts.
2026-09-21 02:47:48 -04:00

98 lines
5.3 KiB
Python

"""Plugin ProviderProfiles resolve wherever a provider NAME is resolved at switch time (#69576).
Discovery is real (a plugin dir under the test HERMES_HOME); only the remote model probe is stubbed.
"""
from __future__ import annotations
import sys
import pytest
import hermes_yaml as yaml
from hermes_cli import auth as auth_mod
from hermes_cli.model_switch import switch_model
from hermes_cli.providers import resolve_provider_full
from hermes_constants import get_hermes_home
KEY = "test-profile-credential"
ACCEPT = {"accepted": True, "persist": True, "recognized": True, "message": None}
@pytest.fixture
def install_profile(monkeypatch):
"""Write one model-provider plugin, rediscover, and mirror it into the auth registry."""
import providers as profiles
monkeypatch.setattr(profiles, "_REGISTRY", dict(profiles._REGISTRY))
monkeypatch.setattr(profiles, "_ALIASES", dict(profiles._ALIASES))
monkeypatch.setattr(profiles, "_PROVIDER_LIST_CACHE", None)
monkeypatch.setattr("agent.models_dev.fetch_models_dev", lambda *a, **kw: {})
monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **kw: ACCEPT)
auth_before = dict(auth_mod.PROVIDER_REGISTRY)
def _install(name: str, *, aliases=(), base_url: str, env_var: str, api_mode: str = "chat_completions"):
monkeypatch.setenv(env_var, KEY)
monkeypatch.setattr(profiles, "_discovered", False)
monkeypatch.delitem(sys.modules, f"_hermes_user_provider_{name}", raising=False)
plugin_dir = get_hermes_home() / "plugins" / "model-providers" / name
plugin_dir.mkdir(parents=True, exist_ok=True)
(plugin_dir / "plugin.yaml").write_text(
f"name: {name}\nkind: model-provider\nversion: 0.0.1\ndescription: switch fixture\n", encoding="utf-8")
(plugin_dir / "__init__.py").write_text(
"from providers import register_provider\nfrom providers.base import ProviderProfile\n"
f"register_provider(ProviderProfile(name={name!r}, aliases={tuple(aliases)!r}, auth_type='api_key',\n"
f" env_vars=({env_var!r},), base_url={base_url!r}, api_mode={api_mode!r}))\n", encoding="utf-8")
auth_mod.sync_plugin_provider_registry()
return profiles.get_provider_profile(name)
yield _install
auth_mod.PROVIDER_REGISTRY.clear()
auth_mod.PROVIDER_REGISTRY.update(auth_before)
def test_alias_switch_carries_the_profiles_canonical_identity(install_profile):
"""`/model --provider <alias>` lands on the profile's own name (persisted config, credential and
runtime lookups all key on it); a user-configured block for that name still wins over the profile."""
install_profile("testgw", aliases=("testgw-alias",), base_url="https://gw.example.test/v1",
env_var="TESTGW_API_KEY", api_mode="anthropic_messages")
pdef = resolve_provider_full("testgw-alias", user_providers={}, custom_providers=[])
assert (pdef.id, pdef.source) == ("testgw", "plugin-profile")
result = switch_model("test-model", current_provider="openrouter", current_model="m",
explicit_provider="testgw-alias", user_providers={}, custom_providers=[])
assert result.success, result.error_message
assert (result.target_provider, result.api_mode, result.api_key) == ("testgw", "anthropic_messages", KEY)
user = {"testgw": {"name": "Configured", "base_url": "https://cfg.example.test/v1", "key_env": "TESTGW_API_KEY"}}
assert resolve_provider_full("testgw-alias", user_providers=user).source == "user-config"
def test_runtime_endpoint_profile_is_a_known_provider_but_placeholders_are_not(install_profile):
"""A profile that mints its endpoint at runtime (empty base_url) resolves on the last rung, so the
switch accepts it; the bare ``custom`` placeholder and its local aliases still fall through to the
custom_providers / current-endpoint handling."""
install_profile("mintgw", base_url="", env_var="MINTGW_API_KEY")
pdef = resolve_provider_full("mintgw", user_providers={}, custom_providers=[])
assert (pdef.id, pdef.base_url, pdef.source) == ("mintgw", "", "plugin-profile")
result = switch_model("test-model", current_provider="openrouter", current_model="m",
explicit_provider="mintgw", user_providers={}, custom_providers=[])
assert result.success, result.error_message
assert (result.target_provider, result.api_key) == ("mintgw", KEY)
for placeholder in ("custom", "ollama", "custom:nope"):
assert resolve_provider_full(placeholder, user_providers={}, custom_providers=[]) is None
def test_persisted_alias_config_applies_to_the_canonical_runtime_provider(install_profile):
"""config.yaml written under an alias (``provider: testgw-alias``) is the same provider at runtime:
its base_url override applies when the canonical name is requested."""
from hermes_cli import runtime_provider as rp
install_profile("testgw", aliases=("testgw-alias",), base_url="https://gw.example.test/v1", env_var="TESTGW_API_KEY")
(get_hermes_home() / "config.yaml").write_text(yaml.safe_dump(
{"model": {"provider": "testgw-alias", "default": "test-model", "base_url": "https://cfg.example.test/v1"}}),
encoding="utf-8")
runtime = rp.resolve_runtime_provider(requested="testgw")
assert (runtime["provider"], runtime["base_url"]) == ("testgw", "https://cfg.example.test/v1")