Skip to content

The agent that grows with you

refactor(providers): host claims + profile_for_host, live context hook; Copilot rows move to its plugin

Tekniuma9234e967126
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index a2a26784c6b..595d0ddaeda 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -3638,14 +3638,14 @@ def _pool_cache_hint(provider: str, *, main_runtime: Optional[dict[str, Any]] =
return f"{normalized}:{entry_id}:{digest}"
-# Ordered (host, provider) tables for inferring a backend from a client base URL.
+# Ordered (host, provider) tables for inferring a backend from a client base URL (then profile ``hostnames``).
_POOL_PROVIDER_BY_HOST = (
("chatgpt.com", "openai-codex"), ("openrouter.ai", "openrouter"),
("inference-api.nousresearch.com", "nous"), ("api.anthropic.com", "anthropic"),
- ("githubcopilot.com", "copilot"), ("api.kimi.com", "kimi-coding"), ("api.x.ai", "xai-oauth"),
+ ("api.kimi.com", "kimi-coding"), ("api.x.ai", "xai-oauth"),
)
_AUTH_REFRESH_PROVIDER_BY_HOST = (
- ("api.githubcopilot.com", "copilot"), ("chatgpt.com", "openai-codex"),
+ ("chatgpt.com", "openai-codex"),
("api.anthropic.com", "anthropic"), ("inference-api.nousresearch.com", "nous"),
# An aux call that inherits the main xai-oauth route arrives as "auto"; without this row the
# 403 bad-credentials rung skipped the refresh and benched the only grant (#84845).
@@ -3654,11 +3654,12 @@ _AUTH_REFRESH_PROVIDER_BY_HOST = (
def _provider_for_host(base_url: str, table: tuple[tuple[str, str], ...]) -> Optional[str]:
- """First provider in ``table`` whose host matches ``base_url``, else None."""
+ """First provider in ``table`` whose host matches ``base_url``, else a profile claiming the host."""
for host, provider in table:
if base_url_host_matches(base_url, host):
return provider
- return None
+ from providers import profile_for_host
+ return getattr(profile_for_host(base_url), "name", None)
def _recoverable_pool_provider(
@@ -5545,7 +5546,6 @@ def _deepinfra_strict_vision_backend(model: Optional[str]) -> tuple[Optional[Any
# through resolve_provider_client so anthropic/* picks wrap onto /v1/messages (a bare _try_nous
# client 404s). openai-codex has no safe default model; callers set auxiliary.<task>.model.
_STRICT_VISION_BACKENDS: dict[str, Callable[[Optional[str]], tuple[Optional[Any], Optional[str]]]] = {
- "copilot": lambda model: resolve_provider_client("copilot", model, is_vision=True),
"openrouter": lambda model: _try_openrouter(model=model),
"nous": lambda model: resolve_provider_client("nous", model, is_vision=True),
"openai-codex": lambda model: resolve_provider_client("openai-codex", model, is_vision=True),
diff --git a/agent/model_metadata.py b/agent/model_metadata.py
index 5126590c7d0..8aa0da3444a 100644
--- a/agent/model_metadata.py
+++ b/agent/model_metadata.py
@@ -438,9 +438,8 @@ def _is_custom_endpoint(base_url: str) -> bool:
return bool(normalized) and not base_url_host_matches(normalized, "openrouter.ai")
-# Host substring -> provider. ".githubcopilot.com" covers api.enterprise./api.business.
-# hosts; models.inference.ai.azure.com (GitHub Models free tier, ~8K per-request cap)
-# is mapped so a targeted hint fires instead of the custom-endpoint path.
+# Host substring -> provider. Profiles claim further hosts through ``hostnames`` /
+# ``metadata_hostnames`` (``providers.profile_for_host``), resolved per home at lookup time.
_URL_TO_PROVIDER: dict[str, str] = {
"api.openai.com": "openai", "chatgpt.com": "openai", "api.anthropic.com": "anthropic",
"api.z.ai": "zai", "open.bigmodel.cn": "zai",
@@ -449,8 +448,6 @@ _URL_TO_PROVIDER: dict[str, str] = {
"dashscope.aliyuncs.com": "alibaba", "dashscope-intl.aliyuncs.com": "alibaba", "portal.qwen.ai": "qwen-oauth",
"openrouter.ai": "openrouter", "generativelanguage.googleapis.com": "gemini",
"inference-api.nousresearch.com": "nous", "api.deepseek.com": "deepseek",
- "api.githubcopilot.com": "copilot", ".githubcopilot.com": "copilot", "models.github.ai": "copilot",
- "models.inference.ai.azure.com": "copilot",
"api.fireworks.ai": "fireworks", "opencode.ai": "opencode-go", "api.x.ai": "xai",
"integrate.api.nvidia.com": "nvidia", "api.xiaomimimo.com": "xiaomi", "xiaomimimo.com": "xiaomi",
"api.gmi-serving.com": "gmi", "api.novita.ai": "novita",
@@ -474,6 +471,9 @@ def _infer_provider_from_url(base_url: str) -> Optional[str]:
if parsed is None:
return None
host = parsed.netloc.lower() or parsed.path.lower()
+ from providers import profile_for_host
+ if (profile := profile_for_host(host, metadata=True)) is not None: # exact claims beat substring rows
+ return profile.name
for url_part, provider in _URL_TO_PROVIDER.items():
if url_part in host:
return provider
@@ -2153,14 +2153,12 @@ def _config_override_context_length(model: str, base_url: str, provider: str, cu
def _resolve_provider_aware_context_length(model: str, base_url: str, api_key: str, provider: str, effective_provider: str) -> Optional[int]:
"""Step 5: provider-specific sources, tried in order; None when all miss."""
- # 5a. Copilot live /models — account-specific models (claude-opus-4.6-1m) absent from
- # models.dev, and the provider-enforced limit for the rest.
- if effective_provider in {"copilot", "copilot-acp", "github-copilot"}:
- with contextlib.suppress(Exception): # fall through to models.dev
- from hermes_cli.models import get_copilot_model_context
- ctx = get_copilot_model_context(model, api_key=api_key)
- if ctx:
- return ctx
+ # 5a. The profile's live, credentialed catalog: account-specific models absent from models.dev.
+ from providers import get_provider_profile
+ profile = get_provider_profile(effective_provider) if effective_provider else None
+ with contextlib.suppress(Exception): # fall through to models.dev
+ if profile is not None and (ctx := profile.live_context_length(model, api_key=api_key or "", base_url=base_url or "")):
+ return ctx
# 5b/5c. Nous portal and Codex OAuth (lower limits than the direct API for the same slug; its
# own /models is authoritative). Persist ONLY the authoritative source ("portal" / "live"): an
# OR-fallback or static-table value cached on a blip would be frozen in by step 1 forever.
@@ -2215,8 +2213,8 @@ def get_model_context_length(
"""Context length for a model. Resolution order: 0 config override / MoA aggregator /
model_overrides / custom_providers / endpoint-scoped; 1 persistent cache (Nous, LM
Studio, Codex OAuth bypass it) and Bedrock; 2-3 custom endpoints (/models, local
- probe, Ollama); 4 Anthropic /v1/models (API keys only); 5 provider-aware (Copilot,
- Nous, Codex OAuth, GMI, Ollama, OpenRouter live, models.dev); 6 OpenRouter for
+ probe, Ollama); 4 Anthropic /v1/models (API keys only); 5 provider-aware (profile live
+ catalog, Nous, Codex OAuth, GMI, Ollama, OpenRouter live, models.dev); 6 OpenRouter for
unknown providers; 7 local server; 8 hardcoded defaults; 9 256K fallback."""
# 0. Explicit config override — user knows best
if isinstance(config_context_length, int) and config_context_length > 0:
@@ -2283,7 +2281,7 @@ def get_model_context_length(
save_context_length(model, base_url, ctx)
return ctx
# 2. Live /models for truly custom endpoints. Known providers skip this: their /models may
- # report a provider-imposed limit (Copilot: 128k) rather than the window. The native
+ # report a provider-imposed limit (e.g. 128k) rather than the window. The native
# openai-codex provider skips it too even on a proxy URL — step 5 runs its live catalog probe.
if _is_custom_endpoint(base_url) and not _is_known_provider_base_url(base_url) and (provider or "").strip().lower() != "openai-codex":
resolve = _resolve_custom_codex_route_context_length if codex_route else _resolve_custom_endpoint_context_length
diff --git a/hermes_cli/models_pricing.py b/hermes_cli/models_pricing.py
index e4daf0d9192..e8b38a936ab 100644
--- a/hermes_cli/models_pricing.py
+++ b/hermes_cli/models_pricing.py
@@ -568,18 +568,22 @@ def resolve_pricing_provider(provider: str, *, base_url: str = "") -> str:
return ""
try:
from agent.model_metadata import _URL_TO_PROVIDER, _infer_provider_from_url
- from utils import base_url_host_matches
+ from providers import get_provider_profile
+ from utils import base_url_host_matches, base_url_hostname
inferred = _infer_provider_from_url(base_url)
+ profile = get_provider_profile(inferred) if inferred else None
except Exception:
return ""
# ``_infer_provider_from_url`` is deliberately broad for user-facing hints. Pricing is a
# trust boundary: require an exact official hostname (or one of its explicit subdomains),
- # never a mere substring such as ``openrouter.ai.attacker.invalid``.
- if not inferred or not any(
+ # never a mere substring such as ``openrouter.ai.attacker.invalid``. A profile's claimed
+ # hosts count only when named exactly.
+ claimed = (*profile.hostnames, *profile.metadata_hostnames) if profile is not None else ()
+ if not inferred or not (base_url_hostname(base_url) in claimed or any(
provider == inferred and not host.startswith(".") and base_url_host_matches(base_url, host)
for host, provider in _URL_TO_PROVIDER.items()
- ):
+ )):
return ""
return normalize_provider(inferred)
diff --git a/plugins/model-providers/copilot-acp/__init__.py b/plugins/model-providers/copilot-acp/__init__.py
index 6027873a84e..1d52cb3cb5c 100644
--- a/plugins/model-providers/copilot-acp/__init__.py
+++ b/plugins/model-providers/copilot-acp/__init__.py
@@ -46,6 +46,13 @@ class CopilotACPProfile(ProviderProfile):
# timeout — the base fetch_models contract is "None if the fetch failed".
return None
+ def live_context_length(self, model: str, *, api_key: str = "", base_url: str = "") -> int | None:
+ """Same account catalog as the HTTP ``copilot`` provider (lane L4 seam)."""
+ from providers import get_provider_profile
+
+ copilot = get_provider_profile("copilot")
+ return copilot.live_context_length(model, api_key=api_key, base_url=base_url) if copilot else None
+
copilot_acp = CopilotACPProfile(
name="copilot-acp", aliases=("github-copilot-acp", "copilot-acp-agent"),
diff --git a/plugins/model-providers/copilot/__init__.py b/plugins/model-providers/copilot/__init__.py
index 745ee4a2814..5aa60bc20e8 100644
--- a/plugins/model-providers/copilot/__init__.py
+++ b/plugins/model-providers/copilot/__init__.py
@@ -10,8 +10,10 @@ from typing import Any
from providers import register_provider
from providers.base import ProviderProfile
+from .catalog import HOSTNAMES, METADATA_HOSTNAMES, MODELS_DEV_ID, CopilotCatalogMixin
-class CopilotProfile(ProviderProfile):
+
+class CopilotProfile(CopilotCatalogMixin, ProviderProfile):
"""GitHub Copilot / GitHub Models — editor headers + reasoning."""
def build_api_kwargs_extras(
@@ -39,6 +41,7 @@ copilot = CopilotProfile(
name="copilot", aliases=("github-copilot", "github-models", "github-model", "github"),
env_vars=("COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"), base_url="https://api.githubcopilot.com",
auth_type="copilot",
+ hostnames=HOSTNAMES, metadata_hostnames=METADATA_HOSTNAMES, models_dev_id=MODELS_DEV_ID,
)
register_provider(copilot)
diff --git a/plugins/model-providers/copilot/catalog.py b/plugins/model-providers/copilot/catalog.py
new file mode 100644
index 00000000000..dd07bb82712
--- /dev/null
+++ b/plugins/model-providers/copilot/catalog.py
@@ -0,0 +1,19 @@
+"""Copilot catalog and model-metadata hooks (lane L4): hosts, models.dev id, live context windows."""
+
+from __future__ import annotations
+
+# Copilot business/enterprise accounts are served from ``api.<plan>.githubcopilot.com``.
+HOSTNAMES = ("githubcopilot.com",)
+# GitHub Models (models.github.ai, and the ~8K-capped free tier at models.inference.ai.azure.com)
+# share the Copilot catalog but take plain GitHub tokens.
+METADATA_HOSTNAMES = ("models.github.ai", "models.inference.ai.azure.com")
+MODELS_DEV_ID = "github-copilot"
+
+
+class CopilotCatalogMixin:
+ """Catalog/metadata hooks for the Copilot profiles."""
+
+ def live_context_length(self, model: str, *, api_key: str = "", base_url: str = "") -> int | None:
+ from hermes_cli.models import get_copilot_model_context
+
+ return get_copilot_model_context(model, api_key=api_key)
diff --git a/providers/__init__.py b/providers/__init__.py
index 5a10c8ea637..61a3ea925ce 100644
--- a/providers/__init__.py
+++ b/providers/__init__.py
@@ -257,6 +257,23 @@ def routed_model_rejects_vision_tool_messages(provider: str, model: str) -> bool
return target_profile is not None and target_profile.supports_vision_tool_messages is False
+def profile_for_host(url: str, *, metadata: bool = False) -> ProviderProfile | None:
+ """The registered profile whose declared ``hostnames`` match *url*'s host (exact or subdomain).
+
+ *metadata* also matches ``metadata_hostnames``. Resolved per call against the bound home's
+ providers, so a ``$HERMES_HOME`` plugin installed after import (or in another profile) is seen.
+ """
+ from utils import base_url_host_matches
+
+ if not url:
+ return None
+ for profile in list_providers():
+ hosts = (*profile.hostnames, *profile.metadata_hostnames) if metadata else profile.hostnames
+ if any(base_url_host_matches(url, host) for host in hosts):
+ return profile
+ return None
+
+
def list_providers() -> list[ProviderProfile]:
"""Return all registered provider profiles (one per canonical name); the bound home's
``$HERMES_HOME`` plugins shadow process-wide profiles of the same name."""
diff --git a/providers/base.py b/providers/base.py
index 90f9f8e15a9..04985ea7d19 100644
--- a/providers/base.py
+++ b/providers/base.py
@@ -536,3 +536,17 @@ class ProviderProfile:
except Exception as exc:
logger.debug("fetch_models(%s): %s", self.name, exc)
return None
+
+ # ── Lane L4: catalog / metadata seams ─────────────────────
+ # Hosts (exact or any subdomain) that serve THIS provider's API, so a ``custom`` endpoint or an
+ # auto-routed aux client at one of them is recognised as this provider (``providers.profile_for_host``).
+ hostnames: tuple = ()
+ # Hosts that identify this provider's catalog for model metadata (context length, models.dev) but
+ # take other credentials; matched only by ``profile_for_host(url, metadata=True)``.
+ metadata_hostnames: tuple = ()
+ # models.dev provider id when it differs from ``name`` (e.g. ``fireworks`` -> ``fireworks-ai``).
+ models_dev_id: str = ""
+
+ def live_context_length(self, model: str, *, api_key: str = "", base_url: str = "") -> int | None:
+ """Context window from the provider's live, credentialed catalog; consulted after the persistent cache."""
+ return None
diff --git a/tests/providers/test_profile_host_seams.py b/tests/providers/test_profile_host_seams.py
new file mode 100644
index 00000000000..8800d7c37cb
--- /dev/null
+++ b/tests/providers/test_profile_host_seams.py
@@ -0,0 +1,70 @@
+"""Host -> profile seams: ``hostnames`` / ``metadata_hostnames`` claims and the live context hook.
+
+A plugin profile claims the hosts that serve its API; URL-based inference (model metadata, auxiliary
+pool/refresh routing) resolves them lazily through ``providers.profile_for_host`` instead of core tables.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+import providers
+from providers.base import ProviderProfile
+
+NAME = "l4-fake-hosted"
+
+
+class _LiveContextProfile(ProviderProfile):
+ def live_context_length(self, model, *, api_key="", base_url=""):
+ self.calls.append((model, api_key, base_url))
+ return 777_000
+
+
+def fake_profile(request):
+ profile = _LiveContextProfile(
+ name=NAME, base_url="https://api.fake-llm.test/v1",
+ hostnames=("fake-llm.test",), metadata_hostnames=("catalog.fake-meta.test",),
+ )
+ profile.calls = []
+ providers.list_providers() # discovery first, so the fixture registration is not shadowed
+ providers.register_provider(profile)
+
+ def _cleanup():
+ providers._REGISTRY.pop(NAME, None)
+ providers._PROVIDER_LIST_CACHE = None
+
+ request.addfinalizer(_cleanup)
+ return profile
+
+
+def test_profile_for_host_matches_claimed_hosts_exact_or_subdomain(fake_profile):
+ assert providers.profile_for_host("https://api.fake-llm.test/v1") is fake_profile
+ assert providers.profile_for_host("https://api.enterprise.fake-llm.test") is fake_profile
+ assert providers.profile_for_host("fake-llm.test") is fake_profile
+ # never a substring: a look-alike host must not inherit the profile
+ assert providers.profile_for_host("https://fake-llm.test.attacker.invalid") is None
+ assert providers.profile_for_host("https://notfake-llm.test") is None
+ # metadata-only hosts need the explicit opt-in
+ assert providers.profile_for_host("https://catalog.fake-meta.test/inference") is None
+ assert providers.profile_for_host("https://catalog.fake-meta.test/inference", metadata=True) is fake_profile
+
+
+def test_claimed_hosts_drive_url_inference_and_aux_routing(fake_profile):
+ from agent.auxiliary_client import _AUTH_REFRESH_PROVIDER_BY_HOST, _POOL_PROVIDER_BY_HOST, _provider_for_host
+ from agent.model_metadata import _infer_provider_from_url
+
+ assert _infer_provider_from_url("https://api.business.fake-llm.test/v1") == NAME
+ assert _infer_provider_from_url("https://catalog.fake-meta.test/inference") == NAME
+ assert _provider_for_host("https://api.fake-llm.test/v1", _POOL_PROVIDER_BY_HOST) == NAME
+ assert _provider_for_host("https://api.fake-llm.test/v1", _AUTH_REFRESH_PROVIDER_BY_HOST) == NAME
+ # credential routing never follows a metadata-only claim (other credentials live there)
+ assert _provider_for_host("https://catalog.fake-meta.test", _POOL_PROVIDER_BY_HOST) is None
+
+
+def test_live_context_length_answers_after_cache_with_credentials(fake_profile):
+ from agent.model_metadata import _resolve_provider_aware_context_length
+
+ ctx = _resolve_provider_aware_context_length("m-1", "https://api.fake-llm.test/v1", "sk-x", NAME, NAME)
+ assert ctx == 777_000
+ assert fake_profile.calls == [("m-1", "sk-x", "https://api.fake-llm.test/v1")]