mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 19:32:45 +03:00
4982ecd8a7
Four resolution-time fallback walkers (runtime_provider.resolve_runtime_with_fallback, cron, the TUI agent factory, the CLI startup fallback) each built the same resolve_runtime_provider kwargs from an entry by hand: provider pin, model, base_url and resolve_entry_api_key. They now call fallback_config.fallback_entry_runtime_kwargs. Session readiness's inline "does this runtime carry a usable credential" test becomes runtime_readiness.runtime_has_credentials. No behaviour change: resolve_requested_provider lowercases the pin, so the two walkers that lowercased it first and the two that did not send the same request. Both helpers exist so `hermes fallback check` (next commit) judges an entry with the switch's own resolution instead of a sixth copy.
85 lines
4.1 KiB
Python
85 lines
4.1 KiB
Python
"""Tests for hermes_cli/fallback_config.py — fallback entry API-key resolution."""
|
|
|
|
from agent.secret_scope import reset_secret_scope, set_secret_scope
|
|
from hermes_cli.fallback_config import effective_runtime_provider, resolve_entry_api_key
|
|
|
|
|
|
class TestResolveEntryApiKey:
|
|
def test_inline_api_key_wins(self, monkeypatch):
|
|
monkeypatch.setenv("FB_KEY", "env-key")
|
|
entry = {"provider": "custom", "api_key": "inline-key", "key_env": "FB_KEY"}
|
|
assert resolve_entry_api_key(entry) == "inline-key"
|
|
|
|
|
|
def test_no_key_fields_returns_none(self):
|
|
assert resolve_entry_api_key({"provider": "openrouter", "model": "glm"}) is None
|
|
|
|
|
|
def test_whitespace_inline_key_falls_through_to_env(self, monkeypatch):
|
|
monkeypatch.setenv("FB_KEY", "env-key")
|
|
entry = {"api_key": " ", "key_env": "FB_KEY"}
|
|
assert resolve_entry_api_key(entry) == "env-key"
|
|
|
|
def test_key_env_resolves_from_active_secret_scope_not_raw_env(self, monkeypatch):
|
|
# Multiplexed gateway: os.environ holds another profile's key, but the
|
|
# active per-turn secret scope holds this profile's key. The scoped
|
|
# value must win — a raw os.getenv() would leak the other profile's
|
|
# credential (issue #74311).
|
|
monkeypatch.setenv("FB_KEY", "fake-other-profile-key")
|
|
token = set_secret_scope({"FB_KEY": "fake-active-profile-key"})
|
|
try:
|
|
assert resolve_entry_api_key({"key_env": "FB_KEY"}) == "fake-active-profile-key"
|
|
finally:
|
|
reset_secret_scope(token)
|
|
|
|
def test_key_env_falls_back_to_env_when_no_active_scope(self, monkeypatch):
|
|
# Non-multiplexed / single-profile behavior must be unchanged: with no
|
|
# secret scope installed, resolution still reads os.environ.
|
|
monkeypatch.setenv("FB_KEY", "env-key")
|
|
assert resolve_entry_api_key({"key_env": "FB_KEY"}) == "env-key"
|
|
|
|
|
|
class TestEffectiveRuntimeProvider:
|
|
"""Named custom fallback entries must keep their configured identity (#98739)."""
|
|
|
|
def test_named_custom_entry_keeps_configured_id(self):
|
|
entry = {"provider": "my-custom-provider", "model": "some-model"}
|
|
runtime = {"provider": "custom", "requested_provider": "my-custom-provider"}
|
|
assert effective_runtime_provider(entry, runtime) == "my-custom-provider"
|
|
|
|
def test_requested_provider_missing_falls_back_to_entry(self):
|
|
entry = {"provider": "my-custom-provider", "model": "some-model"}
|
|
runtime = {"provider": "custom"}
|
|
assert effective_runtime_provider(entry, runtime) == "my-custom-provider"
|
|
|
|
def test_builtin_provider_untouched(self):
|
|
entry = {"provider": "openrouter", "model": "glm"}
|
|
runtime = {"provider": "openrouter", "requested_provider": "openrouter"}
|
|
assert effective_runtime_provider(entry, runtime) == "openrouter"
|
|
|
|
def test_genuinely_bare_custom_stays_custom(self):
|
|
# Ad-hoc endpoint: user literally configured provider: custom.
|
|
entry = {"provider": "custom", "model": "some-model"}
|
|
runtime = {"provider": "custom", "requested_provider": "custom"}
|
|
assert effective_runtime_provider(entry, runtime) == "custom"
|
|
|
|
def test_none_inputs_are_safe(self):
|
|
assert effective_runtime_provider(None, None) == ""
|
|
|
|
|
|
class TestFallbackEntryRuntimeKwargs:
|
|
"""One kwargs shape for every resolution-time walker and ``hermes fallback check``."""
|
|
|
|
def test_carries_provider_model_base_url_and_entry_key(self, monkeypatch):
|
|
from hermes_cli.fallback_config import fallback_entry_runtime_kwargs
|
|
monkeypatch.setenv("FB_KEY", "entry-key")
|
|
assert fallback_entry_runtime_kwargs(
|
|
{"provider": " custom ", "model": " m ", "base_url": "http://h/v1", "key_env": "FB_KEY"}) == {
|
|
"requested": "custom", "target_model": "m", "explicit_base_url": "http://h/v1",
|
|
"explicit_api_key": "entry-key"}
|
|
|
|
def test_omits_absent_endpoint_and_key(self):
|
|
from hermes_cli.fallback_config import fallback_entry_runtime_kwargs
|
|
assert fallback_entry_runtime_kwargs({"provider": "nous", "model": "m"}) == {
|
|
"requested": "nous", "target_model": "m"}
|