route_identity.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """Fail-closed URL identity normalization for model/provider routes."""
  2. from __future__ import annotations
  3. from typing import Any, Optional
  4. from urllib.parse import urlsplit, urlunsplit
  5. def normalize_route_base_url(base_url: Any) -> str:
  6. """Canonicalize only proven-equivalent endpoint URL components."""
  7. raw = str(base_url or "")
  8. if not raw:
  9. return ""
  10. if any(ord(char) <= 0x20 for char in raw):
  11. return raw
  12. had_query_delimiter = "?" in raw.split("#", 1)[0]
  13. try:
  14. parsed = urlsplit(raw)
  15. hostname = parsed.hostname
  16. if not parsed.scheme or not hostname:
  17. return raw
  18. scheme = parsed.scheme.lower()
  19. if "%" in hostname:
  20. address, zone = hostname.split("%", 1)
  21. host = f"{address.lower()}%{zone}"
  22. else:
  23. host = hostname.lower()
  24. port = parsed.port
  25. except (TypeError, ValueError):
  26. return raw
  27. route_host = parsed.netloc.rsplit("@", 1)[-1]
  28. if route_host.startswith("[") or ":" in host:
  29. host = f"[{host}]"
  30. if port is not None and (scheme, port) not in {("http", 80), ("https", 443)}:
  31. host = f"{host}:{port}"
  32. if "@" in parsed.netloc:
  33. host = f"{parsed.netloc.rsplit('@', 1)[0]}@{host}"
  34. path = parsed.path
  35. if path.endswith("/") and not had_query_delimiter:
  36. path = path[:-1]
  37. normalized = urlunsplit((scheme, host, path, parsed.query, ""))
  38. if had_query_delimiter and not parsed.query:
  39. normalized += "?"
  40. return normalized
  41. def provider_owns_route(provider: Any, base_url: Any, config: Any = None) -> Optional[bool]:
  42. """Whether ``model.base_url`` is *provider*'s own endpoint.
  43. ``True``: the host is the provider's registry/plugin endpoint or the endpoint of a
  44. ``providers:``/``custom_providers:`` entry that resolves to *provider*, or *provider* is bare
  45. ``custom``/``local`` (the URL *is* the route). ``False``: the host is another known provider's
  46. stock endpoint, or *provider* names a user entry with a different endpoint. ``None``: unknown
  47. host (a proxy, a LAN server) — nothing here can say whose it is. Offline: the registry lookup
  48. never fetches the models.dev catalog.
  49. """
  50. from hermes_cli.providers import get_provider, normalize_provider, resolve_custom_provider, resolve_user_provider
  51. from utils import base_url_hostname
  52. host = base_url_hostname(str(base_url or ""))
  53. if not host:
  54. return None
  55. raw = str(provider or "").strip().lower()
  56. canonical = normalize_provider(raw)
  57. if canonical in ("custom", "local"):
  58. # Bare custom / local-server aliases are configured BY model.base_url (#14676, #27132);
  59. # resolve_custom_provider would also self-heal bare "custom" to the first entry (#17478).
  60. return True
  61. cfg = config if isinstance(config, dict) else {}
  62. user_pdef = resolve_user_provider(raw, cfg.get("providers") or {}) or resolve_custom_provider(
  63. raw, cfg.get("custom_providers"))
  64. if user_pdef is not None and user_pdef.base_url:
  65. return base_url_hostname(user_pdef.base_url) == host
  66. pdef = get_provider(canonical, allow_network=False)
  67. if pdef is not None and pdef.base_url and base_url_hostname(pdef.base_url) == host:
  68. return True
  69. if pdef is None and user_pdef is None:
  70. return True
  71. from agent.model_metadata import _infer_provider_from_url
  72. inferred = _infer_provider_from_url(str(base_url))
  73. if inferred is None:
  74. return None
  75. return normalize_provider(inferred) == canonical
  76. def drop_stale_model_route(model_cfg: Any, provider: Any, config: Any = None) -> tuple[dict[str, Any], bool]:
  77. """Pop the route keys (``base_url``, ``api_mode``) a previous provider left in ``model:``
  78. when the block is re-pointed at *provider* without a fresh route (``hermes config set
  79. model.provider``). Mirrors what a persisted ``/model`` switch writes: the route is synced to
  80. the target, never carried over. Returns ``(popped {key: old value}, unverified)`` where
  81. ``unverified`` is True when a base_url of unknown ownership was kept — the caller should say so.
  82. A base_url that belongs to *provider* is kept together with its api_mode; with no base_url the
  83. api_mode alone is wire state of the old route and goes."""
  84. if not isinstance(model_cfg, dict):
  85. return {}, False
  86. base_url = str(model_cfg.get("base_url") or "").strip()
  87. owned = provider_owns_route(provider, base_url, config) if base_url else False
  88. if owned:
  89. return {}, False
  90. if owned is None:
  91. return {}, True
  92. popped = {k: model_cfg.pop(k) for k in ("base_url", "api_mode") if model_cfg.get(k) not in (None, "")}
  93. return popped, False
  94. def should_clear_context_pin(configured_model: Any, active_model: Any, configured_base_url: Any, active_base_url: Any,
  95. configured_provider: Any, active_provider: Any) -> bool:
  96. """True when a configured ``model.context_length`` pin no longer matches its runtime route.
  97. Fail-closed: any error during route comparison returns ``True`` (drop the pin) so a stale window
  98. never silently inflates the compression threshold."""
  99. configured_model = str(configured_model or "").strip()
  100. if configured_model and configured_model != str(active_model or "").strip():
  101. return True
  102. try:
  103. from agent.agent_init import _context_route_mismatch
  104. return _context_route_mismatch(configured_base_url, active_base_url, configured_provider, active_provider)
  105. except Exception:
  106. return True
  107. async def should_clear_context_pin_async(*args: Any) -> bool:
  108. """``should_clear_context_pin`` on a worker thread so async gateway handlers never run it on the
  109. event loop — the resolution chain is cache-only (``allow_network=False``) but can still do
  110. cold-start disk I/O."""
  111. import asyncio
  112. return await asyncio.to_thread(should_clear_context_pin, *args)