Skip to content

The agent that grows with you

feat(gateway): one resolved-route identity for launch-route gates

Tekniumb0bee4626b8e
diff --git a/gateway/session_route_identity.py b/gateway/session_route_identity.py
new file mode 100644
index 00000000000..88bc59c05ec
--- /dev/null
+++ b/gateway/session_route_identity.py
@@ -0,0 +1,53 @@
+"""One resolved-route identity for launch-route gates.
+
+Two launch routes are the same route when the runtime resolver would send them to the same
+endpoint: compare what it resolves (canonical provider, configured named entry, effective
+URL), never raw spellings. ``ollama``/``vllm`` resolve to ``custom``, ``deep-infra`` to
+``deepinfra``, and a configured ``providers:``/``custom_providers`` name (or its
+``custom:<name>`` spelling) to that entry.
+"""
+
+
+def _named_entry(name: str, config: dict) -> dict | None:
+ from hermes_cli.runtime_provider_custom import _get_named_custom_provider
+ return _get_named_custom_provider(name, config=config, resolve_credentials=False)
+
+
+def _canonical(name: str) -> str:
+ from hermes_cli.models import normalize_provider
+ canonical = normalize_provider(name)
+ from hermes_cli.auth import AuthError, resolve_provider
+ try:
+ return resolve_provider(canonical)
+ except AuthError: # not a registry provider: its canonical spelling is its identity
+ return canonical
+
+
+def resolved_route(model_cfg: dict, config: dict) -> tuple[str, str]:
+ """``(provider_identity, effective_base_url)`` the runtime resolves ``model_cfg`` to."""
+ model_cfg = model_cfg if isinstance(model_cfg, dict) else {}
+ config = config if isinstance(config, dict) else {}
+ name = str(model_cfg.get('provider') or '').strip().lower()
+ if name in ('', 'auto'):
+ return 'auto', ''
+ base_url = str(model_cfg.get('base_url') or '').strip()
+ entry = _named_entry(name, config)
+ if entry:
+ from hermes_cli.providers import custom_provider_slug
+ identity = custom_provider_slug(str(entry.get('name') or ''), str(entry.get('provider_key') or ''))
+ base_url = base_url or str(entry.get('base_url') or '').strip()
+ else:
+ identity = _canonical(name)
+ return identity, base_url.rstrip('/')
+
+
+def same_route(a_cfg: dict, b_cfg: dict, config: dict) -> bool:
+ """True when both configs resolve to one endpoint; ``auto`` on either side keeps the route."""
+ a_id, a_url = resolved_route(a_cfg, config)
+ b_id, b_url = resolved_route(b_cfg, config)
+ if 'auto' in (a_id, b_id):
+ return True
+ if a_url or b_url:
+ from hermes_cli.route_identity import normalize_route_base_url
+ return normalize_route_base_url(a_url) == normalize_route_base_url(b_url)
+ return a_id == b_id
diff --git a/tests/gateway/test_session_route_identity.py b/tests/gateway/test_session_route_identity.py
new file mode 100644
index 00000000000..ee0faf9c54a
--- /dev/null
+++ b/tests/gateway/test_session_route_identity.py
@@ -0,0 +1,32 @@
+"""Launch-route gates compare what the runtime resolves, not how the provider was spelled."""
+import pytest
+
+from gateway.session_route_identity import resolved_route, same_route
+
+A, B = 'http://127.0.0.1:8001/v1', 'http://127.0.0.1:8002/v1'
+CONFIG = {'providers': {'team': {'base_url': A, 'api_key': 'sk-team'}}}
+
+
[email protected]('a, b, config, expected', [
+ ({'provider': 'vllm', 'base_url': A}, {'provider': 'custom', 'base_url': A + '/'}, {}, True),
+ ({'provider': 'ollama', 'base_url': A}, {'provider': 'custom', 'base_url': A}, {}, True),
+ ({'provider': 'team'}, {'provider': 'custom:team'}, CONFIG, True),
+ ({'provider': 'team'}, {'provider': 'custom:team', 'base_url': A}, CONFIG, True),
+ ({'provider': 'team'}, {'provider': 'custom:team', 'base_url': B}, CONFIG, False),
+ ({'provider': 'team'}, {'provider': 'team'},
+ {'providers': {'team': {'base_url': B}}}, True),
+ ({'provider': 'auto'}, {'provider': 'custom', 'base_url': B}, {}, True),
+ ({'provider': 'deepinfra'}, {'provider': 'deep-infra'}, {}, True),
+ ({'provider': 'openrouter'}, {'provider': 'anthropic'}, {}, False),
+ ({'provider': 'custom', 'base_url': A}, {'provider': 'custom', 'base_url': B}, {}, False),
+])
+def test_same_route(a, b, config, expected):
+ assert same_route(a, b, config) is expected
+ assert same_route(b, a, config) is expected
+
+
+def test_resolved_route_identity_and_url():
+ assert resolved_route({'provider': 'custom:team'}, CONFIG) == ('custom:team', A)
+ assert resolved_route({'provider': 'TEAM', 'base_url': B + '/'}, CONFIG) == ('custom:team', B)
+ assert resolved_route({'provider': 'ollama', 'base_url': A}, {}) == ('custom', A)
+ assert resolved_route({'provider': ''}, CONFIG) == ('auto', '')