The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: hermes doctor probes Azure Foundry Anthropic endpoints like the runtime
Bartok99bd66000a746
diff --git a/hermes_cli/doctor_connectivity.py b/hermes_cli/doctor_connectivity.pyindex bdd0ace0170..a99af007598 100644--- a/hermes_cli/doctor_connectivity.py+++ b/hermes_cli/doctor_connectivity.py@@ -184,7 +184,15 @@ def _probe_apikey_provider(pname, env_vars, default_url, base_env, supports_healtry:import httpxbase, url, headers = _apikey_request(key, base_env, default_url)- r = httpx.get(url, headers=headers, timeout=10)+ if base.rstrip("/").endswith("/anthropic"):+ # Anthropic-only gateway (no OpenAI-compat sibling, so no /models): probe the route the runtime uses.+ r = _anthropic_messages_probe(base, key)+ if r.status_code == 400: # Anthropic-shaped 400 still proves route + auth (#66756)+ return _row(pname, "ok", label=label)+ if r.status_code == 403:+ return _row(pname, "fail", "(access denied)", [f"Check {env_vars[0]} in .env"], label=label)+ else:+ r = httpx.get(url, headers=headers, timeout=10)if pname == "Alibaba/DashScope" and not base and r.status_code == 401:r = httpx.get("https://dashscope.aliyuncs.com/compatible-mode/v1/models", headers=headers, timeout=10)except Exception as e:@@ -194,9 +202,36 @@ def _probe_apikey_provider(pname, env_vars, default_url, base_env, supports_healreturn _row(pname, "ok", label=label) if r.status_code == 200 else _row(pname, "warn", f"(HTTP {r.status_code})", label=label)+def _anthropic_messages_probe(base: str, key: str):+ """POST ``<base>/v1/messages`` with ``max_tokens=1`` exactly as the Anthropic adapter would: same+ auth family (Bearer for Azure Foundry, else x-api-key) and the same ``api-version`` query. Azure+ Foundry's ``/anthropic`` route 404s on ``GET /models`` even when chat works (#66756)."""+ import httpx+ from agent.anthropic_adapter import _base_client_kwargs+ from agent.anthropic_endpoints import _requires_bearer_auth+ normalized, kwargs = _base_client_kwargs(base, None)+ auth = {"Authorization": f"Bearer {key}"} if _requires_bearer_auth(normalized) else {"x-api-key": key}+ headers = {"anthropic-version": "2023-06-01", "User-Agent": _HERMES_USER_AGENT, **auth}+ model = str(_model_cfg().get("default") or "").strip() or "claude-sonnet-4-5"+ body = {"model": model, "max_tokens": 1, "messages": [{"role": "user", "content": "ping"}]}+ return httpx.post(normalized + "/v1/messages", headers=headers, params=kwargs.get("default_query"), json=body, timeout=10)+++def _model_cfg() -> dict:+ try:+ from hermes_cli.config import load_config_readonly+ model_cfg = (load_config_readonly() or {}).get("model")+ except Exception:+ return {}+ return model_cfg if isinstance(model_cfg, dict) else {}++def _apikey_request(key: str, base_env, default_url) -> tuple:"""(effective base, models URL, headers) for a generic Bearer-auth probe, with the per-vendor rewrites."""base = os.getenv(base_env, "") if base_env else ""+ # Azure Foundry's base URL is per-resource and normally lives in config (model.base_url), not the env var.+ if not base and base_env == "AZURE_FOUNDRY_BASE_URL" and str(_model_cfg().get("provider") or "").strip().lower() == "azure-foundry":+ base = str(_model_cfg().get("base_url") or "").strip()# Kimi Code keys (sk-kimi-) → api.kimi.com/coding/v1 (OpenAI-compat surface exposing /models).if not base and key.startswith("sk-kimi-"):base = "https://api.kimi.com/coding/v1"diff --git a/tests/hermes_cli/test_doctor_azure_foundry_probe.py b/tests/hermes_cli/test_doctor_azure_foundry_probe.pynew file mode 100644index 00000000000..f9154dad45f--- /dev/null+++ b/tests/hermes_cli/test_doctor_azure_foundry_probe.py@@ -0,0 +1,61 @@+"""``hermes doctor`` connectivity probe for Azure Foundry Anthropic-style endpoints (#66756).++The ``/anthropic`` route on Foundry has no ``GET /models``; a working deployment answered the generic+Bearer ``/models`` probe with HTTP 404. The probe must follow the runtime protocol instead:+``POST <base>/v1/messages`` with Bearer auth and the ``api-version`` query the Anthropic adapter sends.+"""++from __future__ import annotations++import httpx++from hermes_cli import doctor_connectivity as dc++_AZURE_BASE = "https://res.services.ai.azure.com/anthropic"+++def _run_probe(monkeypatch, status: int, base_url_in_env: bool):+ calls: list = []++ def _post(url, headers=None, params=None, json=None, timeout=None):+ calls.append(("POST", url, headers, params, json))+ return httpx.Response(status, json={"type": "error", "error": {"type": "invalid_request_error", "message": "x"}}+ if status == 400 else {"id": "msg_1"})++ def _get(url, headers=None, timeout=None):+ calls.append(("GET", url, headers, None, None))+ return httpx.Response(404)++ monkeypatch.setattr(httpx, "post", _post)+ monkeypatch.setattr(httpx, "get", _get)+ monkeypatch.setenv("AZURE_FOUNDRY_API_KEY", "k")+ if base_url_in_env:+ monkeypatch.setenv("AZURE_FOUNDRY_BASE_URL", _AZURE_BASE)+ else:+ monkeypatch.delenv("AZURE_FOUNDRY_BASE_URL", raising=False)+ monkeypatch.setattr(dc, "_model_cfg", lambda: {"provider": "azure-foundry", "base_url": _AZURE_BASE, "default": "claude-sonnet-5"})+ res = dc._probe_apikey_provider("Azure Foundry", ("AZURE_FOUNDRY_API_KEY", "AZURE_FOUNDRY_BASE_URL"), None,+ "AZURE_FOUNDRY_BASE_URL", True)+ return res, calls+++def test_anthropic_style_foundry_probe_posts_messages_like_the_runtime(monkeypatch):+ """Healthy row, one POST /v1/messages with Bearer auth + api-version + max_tokens=1 — never GET /models."""+ for status in (200, 400):+ res, calls = _run_probe(monkeypatch, status, base_url_in_env=True)+ assert res.issues == [] and "\u2713" in res.lines[0][0], (status, res)+ assert [c[0] for c in calls] == ["POST"]+ _, url, headers, params, body = calls[0]+ assert url == _AZURE_BASE + "/v1/messages"+ assert headers["Authorization"] == "Bearer k" and "x-api-key" not in headers+ assert headers["anthropic-version"] == "2023-06-01"+ assert params == {"api-version": "2025-04-15"}+ assert body["max_tokens"] == 1 and body["model"] == "claude-sonnet-5"+++def test_foundry_base_url_falls_back_to_config_and_404_stays_visible(monkeypatch):+ """Base URL only in ``model.base_url`` is still probed; a real 404 on /v1/messages is still reported."""+ res, calls = _run_probe(monkeypatch, 200, base_url_in_env=False)+ assert calls[0][1] == _AZURE_BASE + "/v1/messages" and res.issues == []+ res, _ = _run_probe(monkeypatch, 404, base_url_in_env=True)+ assert "HTTP 404" in res.lines[0][2]diff --git a/website/docs/guides/azure-foundry.md b/website/docs/guides/azure-foundry.mdindex 6276e728848..baff0a9950e 100644--- a/website/docs/guides/azure-foundry.md+++ b/website/docs/guides/azure-foundry.md@@ -248,6 +248,7 @@ Important behaviour:- **Bearer auth is used instead of `x-api-key`.** Azure's Anthropic-compatible route requires `Authorization: Bearer <key>` rather than Anthropic's native `x-api-key` header. Hermes detects `azure.com` in the base URL and routes the API key through the SDK's `auth_token` field so the right header reaches the upstream.- **1M context window beta header is kept.** Azure still gates the 1M-token Claude context (Opus 4.6/4.7, Sonnet 4.6) behind the `anthropic-beta: context-1m-2025-08-07` header. Hermes keeps that beta header on Azure paths (it's stripped from native Anthropic OAuth requests because some subscriptions reject it, but Azure requires it).- **OAuth token refresh is disabled.** Azure deployments use static API keys. The `~/.claude/.credentials.json` OAuth token refresh loop that applies to Anthropic Console is explicitly skipped for Azure endpoints to prevent the Claude Code OAuth token from overwriting your Azure key mid-session.+- **`hermes doctor` probes the same route.** The `/anthropic` route has no `GET /models`, so the connectivity check sends a one-token `POST /v1/messages` with the same Bearer auth and `api-version` query the runtime uses; a 200 (or a 400 from the Messages API) reports the endpoint as healthy, 401/403 as an auth problem.## Alternative: `provider: anthropic` + Azure base URL