The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): compare endpoints for session identity without folding path case
yoniebansf3b5b980d615
diff --git a/hermes_cli/runtime_provider_custom.py b/hermes_cli/runtime_provider_custom.pyindex 4687d989fe7..908516b0b47 100644--- a/hermes_cli/runtime_provider_custom.py+++ b/hermes_cli/runtime_provider_custom.py@@ -35,6 +35,12 @@ def _normalize_base_url_for_match(value) -> str:return str(value or "").strip().rstrip("/").lower()+def _route_key(value) -> str:+ """An endpoint compared for identity: scheme and host fold case, the path does not."""+ from hermes_cli.route_identity import normalize_route_base_url+ return normalize_route_base_url(_clean(value))++def _clean(value: Any) -> str:return str(value or "").strip()@@ -320,10 +326,10 @@ def find_custom_provider_identity(base_url: str) -> Optional[str]:"""Map an endpoint URL back to its canonical ``custom:<name>`` menu key. Session persistencestores the agent's *resolved* provider, which for every named custom endpoint is the literalstring ``"custom"`` — the entry name is lost, and the api_key is deliberately never persisted."""- target = _normalize_base_url_for_match(base_url)+ target = _route_key(base_url)if not target:return None- return _find_custom_identity(lambda entry: _normalize_base_url_for_match(_entry_url(entry)) == target)+ return _find_custom_identity(lambda entry: _route_key(_entry_url(entry)) == target)def _model_id_matches(value: Any, target: str) -> bool:@@ -361,7 +367,7 @@ def _endpoint_identity(base_url: str) -> Optional[str]:# from the ownership-checked endpoint, never from a model name or a fixed port.from hermes_cli.local_runtime.endpoint import _state_endpointendpoint = _state_endpoint()- if endpoint and _normalize_base_url_for_match(base_url) == _normalize_base_url_for_match(endpoint["base_url"]):+ if endpoint and _route_key(base_url) == _route_key(endpoint["base_url"]):return "llamacpp"return None@@ -416,10 +422,10 @@ def session_provider_identity(provider: Any, requested_provider: Any, base_url:if adapter.lower() != "custom":return adapterrequested = _clean(requested_provider)- url = _normalize_base_url_for_match(base_url)+ url = _route_key(base_url)if url and requested.lower() not in {"", "custom", "auto"}:entry = _get_named_custom_provider(requested, config=config, resolve_credentials=False)- if entry and _normalize_base_url_for_match(_entry_url(entry)) == url:+ if entry and _route_key(_entry_url(entry)) == url:return requestedif url:return _endpoint_identity(_clean(base_url)) or adapterdiff --git a/tests/gateway/test_named_custom_provider_session_identity.py b/tests/gateway/test_named_custom_provider_session_identity.pyindex 0d255220bd1..bd8b6a3f0b4 100644--- a/tests/gateway/test_named_custom_provider_session_identity.py+++ b/tests/gateway/test_named_custom_provider_session_identity.py@@ -159,7 +159,7 @@ OLD_URL = "https://old.example/v1"NEW_URL = "https://new.example/v1"-def _edited_entry_agent(*, old_owner=True):+def _edited_entry_agent(*, old_owner=True, old_url=OLD_URL, new_url=NEW_URL):"""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_provider@@ -168,15 +168,15 @@ def _edited_entry_agent(*, old_owner=True):config = {"model": {"default": "m", "provider": "custom:alpha"},"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"},+ {"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")agent = SimpleNamespace(model="m", provider=runtime["provider"], requested_provider=runtime["requested_provider"],base_url=runtime["base_url"], api_mode=runtime["api_mode"], _fallback_activated=False)- config["custom_providers"][0].update(base_url=NEW_URL, api_key="sk-new")+ config["custom_providers"][0].update(base_url=new_url, api_key="sk-new")atomic_config_write(get_config_path(), config)return agent@@ -208,22 +208,25 @@ 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."""[email protected]("old_url,new_url", [(OLD_URL, NEW_URL), ("https://relay.example/TenantA/v1", "https://relay.example/tenanta/v1")],+ ids=["new-host", "path-case-only"])+def test_moved_entry_with_no_old_owner_is_not_saved_beside_the_old_endpoint(tmp_path, old_url, new_url):+ """Neither the requested-entry check nor the endpoint-owner and model fallbacks hand the moved+ entry's name back for an endpoint it left; a path differing only in case is another endpoint."""import json- agent = _edited_entry_agent(old_owner=False)+ agent = _edited_entry_agent(old_owner=False, old_url=old_url, new_url=new_url)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")+ 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")+ assert _resumed_route(row) != (old_url, "sk-new")def test_live_unpersisted_session_reports_the_requested_entry(monkeypatch):