The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): never name a moved entry for the endpoint it left
yoniebans69d5e0e458c6
diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.pyindex 821de6e27c0..4687d989fe7 100644--- a/hermes_cli/runtime_provider_custom.py+++ b/hermes_cli/runtime_provider_custom.py@@ -352,22 +352,29 @@ def find_custom_provider_identity_by_model(model: str) -> Optional[str]:return _find_custom_identity(_entry_serves_model)+def _endpoint_identity(base_url: str) -> Optional[str]:+ """The menu identity that owns ``base_url``: a configured entry, else the managed server."""+ identity = find_custom_provider_identity(base_url)+ if identity:+ return identity+ # The managed server has no custom-provider config entry. Recover its menu key+ # from the ownership-checked endpoint, never from a model name or a fixed port.+ from hermes_cli.local_runtime.endpoint import _state_endpoint+ endpoint = _state_endpoint()+ if endpoint and _normalize_base_url_for_match(base_url) == _normalize_base_url_for_match(endpoint["base_url"]):+ return "llamacpp"+ return None++def canonical_custom_identity(*, base_url: Optional[str] = None, config_provider: Optional[str] = None,model: Optional[str] = None) -> Optional[str]:"""Recover the durable menu identity for a bare custom provider. Match a configuredendpoint first, then the ownership-checked managed server, then a configured model orprovider. Every session persistence/restore path shares this lookup."""rp = _rp()- if base_url:- identity = find_custom_provider_identity(base_url)- if identity:- return identity- # The managed server has no custom-provider config entry. Recover its menu key- # from the ownership-checked endpoint, never from a model name or a fixed port.- from hermes_cli.local_runtime.endpoint import _state_endpoint- endpoint = _state_endpoint()- if endpoint and _normalize_base_url_for_match(base_url) == _normalize_base_url_for_match(endpoint["base_url"]):- return "llamacpp"+ identity = _endpoint_identity(base_url) if base_url else None+ if identity:+ return identityidentity = find_custom_provider_identity_by_model(model) if model else Noneif identity:return identity@@ -402,8 +409,9 @@ def canonical_custom_identity(*, base_url: Optional[str] = None, config_providerdef session_provider_identity(provider: Any, requested_provider: Any, base_url: Any, model: Any = None, *,config: Optional[dict[str, Any]] = None) -> str:- """A session's identity: the requested entry while it still serves ``base_url`` (an edited entry would- pair its new key with the old endpoint on resume), else the endpoint's entry, else the adapter."""+ """A session's identity: the requested entry while it still serves ``base_url``, else the endpoint's+ owner, else the adapter. A name for an entry that left ``base_url`` pairs its new key with the old+ endpoint on resume, so with an endpoint known the model and config fallbacks never apply."""adapter = _clean(provider)if adapter.lower() != "custom":return adapter@@ -413,7 +421,9 @@ def session_provider_identity(provider: Any, requested_provider: Any, base_url:entry = _get_named_custom_provider(requested, config=config, resolve_credentials=False)if entry and _normalize_base_url_for_match(_entry_url(entry)) == url:return requested- return canonical_custom_identity(base_url=_clean(base_url) or None, model=_clean(model) or None) or adapter+ if url:+ return _endpoint_identity(_clean(base_url)) or adapter+ return canonical_custom_identity(model=_clean(model) or None) or adapterdef is_routable_provider(provider: Optional[str]) -> bool:diff --git a/tests/gateway/test_named_custom_provider_session_identity.py b/tests/gateway/test_named_custom_provider_session_identity.pyindex 055a7d6be88..0d255220bd1 100644--- a/tests/gateway/test_named_custom_provider_session_identity.py+++ b/tests/gateway/test_named_custom_provider_session_identity.py@@ -159,8 +159,9 @@ OLD_URL = "https://old.example/v1"NEW_URL = "https://new.example/v1"-def _edited_entry_agent():- """A live session on ``custom:alpha`` whose entry was then moved to another endpoint and key."""+def _edited_entry_agent(*, old_owner=True):+ """A live session on ``custom:alpha`` whose entry was then moved to another endpoint and key; with+ ``old_owner`` another entry still serves the old endpoint, without it nothing does."""from hermes_cli import runtime_providerfrom hermes_cli.config import atomic_config_write, get_config_path@@ -169,7 +170,7 @@ def _edited_entry_agent():"custom_providers": [{"name": "alpha", "base_url": OLD_URL, "api_key": "sk-old", "model": "m"},{"name": "old-owner", "base_url": OLD_URL, "api_key": "sk-old", "model": "m"},- ],+ ][:2 if old_owner else 1],}atomic_config_write(get_config_path(), config)runtime = runtime_provider.resolve_runtime_provider(requested="custom:alpha", target_model="m")@@ -207,6 +208,24 @@ def test_tui_runtime_config_does_not_pair_an_edited_entry_key_with_the_old_endpoassert _resumed_route(row) == (OLD_URL, "sk-old")+def test_moved_entry_with_no_old_owner_is_not_saved_beside_the_old_endpoint(tmp_path):+ """The model-name fallback must not hand the moved entry's name back for an endpoint it left."""+ import json++ agent = _edited_entry_agent(old_owner=False)+ assert _runtime_model_config(agent)["provider"] == "custom"+ db = SessionDB(tmp_path / "state.db")+ try:+ db.create_session("s1", source="telegram", model="m")+ GatewayTurnPrepareMixin._sync_session_model_from_agent(_runner(db), "s1", agent)+ assert SessionDB.session_gateway_runtime(db.get_session("s1"))["provider"] == "custom"+ assert _resumed_route(db.get_session("s1")) != (OLD_URL, "sk-new")+ finally:+ db.close()+ row = {"model": "m", "model_config": json.dumps(_runtime_model_config(agent))}+ assert _resumed_route(row) != (OLD_URL, "sk-new")++def test_live_unpersisted_session_reports_the_requested_entry(monkeypatch):"""A built session reattached before its first turn reports the entry it runs, not the adapter class."""from hermes_cli.config import atomic_config_write, get_config_path