Skip to content

The agent that grows with you

refactor(anthropic): split the adapter godfile into four modules

joaomarcos7cbffdd12547
diff --git a/ANTHROPIC_ISSUES.md b/ANTHROPIC_ISSUES.md
index b0293ed9e47..b805ab5afcf 100644
--- a/ANTHROPIC_ISSUES.md
+++ b/ANTHROPIC_ISSUES.md
@@ -195,6 +195,45 @@ if msvcrt and (not lock_path.exists() or lock_path.stat().st_size == 0):
---
+## Bug 6 — Rotação single-use confirmada como sucesso sem commit durável
+
+**Onde:** `agent/anthropic_credentials.py::_write_claude_code_credentials` e
+`::_write_hermes_oauth_credentials` (antes em `agent/anthropic_adapter.py`),
+consumidos por `_refresh_oauth_token()` e por
+`agent/credential_pool.py::_refresh_entry_impl()`.
+
+Os dois escritores capturavam `OSError`/`IOError`, logavam em nível debug e não
+devolviam sinal de falha. Nenhum chamador conseguia distinguir um commit durável
+de uma escrita perdida.
+
+O refresh token da Anthropic é *single-use*: o POST que devolve o par novo já
+invalida o que foi enviado. O par novo só passa a existir de fato quando chega
+ao seu store autoritativo — `~/.claude/.credentials.json` para `claude_code`,
+`~/.hermes/.anthropic_oauth.json` para `hermes_pkce`. Como
+`credential_pool._seed_from_singletons()` relê esses arquivos em todo
+`load_pool()` e sobrescreve a linha do pool com o que encontrar, uma escrita
+falha deixava o par **já consumido** no disco: o próximo processo re-semeava o
+par gasto por cima da linha nova e o refresh seguinte reenviava um token
+consumido (`invalid_grant` / `refresh_token_reused`).
+
+**Correção:** os dois escritores levantam `CredentialPersistError`; o resolver
+direto trata commit falho como refresh falho; `_refresh_entry_impl()` falha
+fechado nos caminhos primário e de retry, nunca marcando, persistindo ou
+devolvendo a rotação, e coloca a entrada em quarentena `DEAD` com razão
+`credential_persist_failed` para que ela saia da rotação em vez de cair
+silenciosamente em outro provedor. O caminho de retry passou a commitar no
+singleton **antes** de persistir a linha do pool.
+
+Um segundo defeito apareceu ao cobrir isso: `_upsert_entry()` tratava cada
+re-semeadura de uma fonte *borrowed* como rotação de chave. Linhas borrowed
+(`claude_code`, fontes de env) vão para o `auth.json` sem o segredo, então
+comparar o token re-semeado com o valor vazio guardado reportava rotação em
+todo load e limpava o estado `DEAD` recém-escrito — ressuscitando na
+reinicialização exatamente a credencial em quarentena. Agora a comparação usa o
+`secret_fingerprint` da linha.
+
+Cobertura: `tests/agent/test_anthropic_credential_persist_failure.py`.
+
## Zombie process — investigado, não confirmado
Checado especificamente no fluxo Anthropic:
@@ -216,12 +255,13 @@ Checado especificamente no fluxo Anthropic:
| `tests/agent/test_anthropic_keychain.py` | Resolver direto Claude Code sob lock compartilhado |
| `tests/hermes_cli/test_auth_store_lock_concurrent.py` | Concorrência do lock real, marcada `windows_only` |
| `tests/agent/test_credential_pool_oauth_writethrough.py` | Rotação `hermes_pkce` preservada no `.anthropic_oauth.json` |
+| `tests/agent/test_anthropic_credential_persist_failure.py` | Injeção de falha no *commit* do refresh: escritores, resolver direto, `claude_code`, `hermes_pkce` e caminho de retry |
| `RELATORIO_ANTHROPIC_OAUTH_BUGS.md` | Relatório técnico histórico e estado da implementação |
| `ANTHROPIC_ISSUES.md` | Este arquivo — resumo consolidado e matriz de escopo |
**Nota sobre dados sensíveis:** este relatório foi verificado com busca por regex de chaves reais (`sk-ant-...`, `ANTHROPIC_API_KEY=`, `ANTHROPIC_TOKEN=`) — nenhuma encontrada. Só há valores sintéticos usados nos testes automatizados (ex: `sk-ant-oat-rotated-1`), nunca uma chave real. O diagnóstico do Bug 4 pede pro usuário rodar `hermes doctor` / checar o próprio `.env` localmente, mas o resultado desse comando **não foi colado neste relatório** — só a explicação da causa raiz no código.
-Arquivos de produção alterados pelo PR: `agent/anthropic_adapter.py`, `agent/credential_pool.py`, `hermes_cli/auth.py` e `hermes_cli/web_server.py`. O frontend também recebe a correção de escopo em `web/src/lib/api.ts` para que operações OAuth sigam o perfil selecionado.
+Arquivos de produção alterados pelo PR: `agent/anthropic_adapter.py` (agora dividido em `agent/anthropic_endpoints.py`, `agent/anthropic_message_convert.py` e `agent/anthropic_credentials.py`), `agent/credential_pool.py`, `agent/auxiliary_client.py`, `hermes_cli/auth.py` e `hermes_cli/web_server.py`. O frontend também recebe a correção de escopo em `web/src/lib/api.ts` para que operações OAuth sigam o perfil selecionado.
## Como validar o estado atual
@@ -244,5 +284,8 @@ Os resultados dependem do ambiente e devem ser registrados no PR junto com a cab
|---|---|
| `hermes_cli/web_server.py` | Catálogo `anthropic` como `flow: "external"`; dispatcher rejeita `start`/`submit` |
| `agent/credential_pool.py` | `_refresh_entry`, `_sync_anthropic_entry_from_pool_store`, lock compartilhado Claude Code e fallback de recuperação |
-| `agent/anthropic_adapter.py` | `claude_code_credentials_path`, refresh puro, fluxo CLI PKCE separado e write-through Hermes |
+| `agent/anthropic_credentials.py` | `claude_code_credentials_path`, refresh puro, fluxo CLI PKCE separado, write-through Hermes e `CredentialPersistError` |
+| `agent/anthropic_endpoints.py` | Predicados de família de endpoint por base URL |
+| `agent/anthropic_message_convert.py` | Conversão de payload OpenAI → Anthropic |
+| `agent/anthropic_adapter.py` | Construção de cliente e chamada da Messages API; re-exporta os três módulos acima |
| `web/src/lib/api.ts` | `/api/providers/oauth` incluído no escopo de perfil do dashboard |
diff --git a/RELATORIO_ANTHROPIC_OAUTH_BUGS.md b/RELATORIO_ANTHROPIC_OAUTH_BUGS.md
index ed18f221a45..80456bce311 100644
--- a/RELATORIO_ANTHROPIC_OAUTH_BUGS.md
+++ b/RELATORIO_ANTHROPIC_OAUTH_BUGS.md
@@ -247,7 +247,10 @@ O teste `test_distinct_profiles_share_one_claude_refresh_without_duplicate_post`
|---|---|
| `hermes_cli/web_server.py` | Catálogo `anthropic` como `flow: "external"`; dispatcher rejeita `start`/`submit` |
| `agent/credential_pool.py` | `_refresh_entry`, `_sync_anthropic_entry_from_pool_store`, lock compartilhado Claude Code e recuperação |
-| `agent/anthropic_adapter.py` | `claude_code_credentials_path`, refresh puro e fluxo CLI PKCE separado |
+| `agent/anthropic_credentials.py` | `claude_code_credentials_path`, refresh puro, fluxo CLI PKCE separado e `CredentialPersistError` |
+| `agent/anthropic_endpoints.py` | Predicados de família de endpoint por base URL |
+| `agent/anthropic_message_convert.py` | Conversão de payload OpenAI → Anthropic |
+| `agent/anthropic_adapter.py` | Construção de cliente e chamada da Messages API; re-exporta os três módulos acima |
| `web/src/lib/api.ts` | `/api/providers/oauth` incluído no escopo de perfil |
---
@@ -275,3 +278,46 @@ O fluxo dashboard foi removido depois dessa validação. O que permanece
verificável localmente é o fluxo de terminal e a ausência das rotas dashboard;
uma validação em uma instalação publicada não foi executada nesta revisão e
depende de distribuir uma versão que contenha a cabeça final do PR.
+
+---
+
+## 8. Commit do refresh como parte da transação (2026-08-27)
+
+O refresh token da Anthropic é single-use: o POST que devolve o par novo
+invalida o que foi enviado. O par novo só existe de fato quando chega ao store
+autoritativo — `~/.claude/.credentials.json` (`claude_code`) ou
+`~/.hermes/.anthropic_oauth.json` (`hermes_pkce`). Esses arquivos são
+autoritativos no sentido estrito: `credential_pool._seed_from_singletons()`
+os relê em todo `load_pool()` e escreve o que encontra por cima da linha do
+pool.
+
+Até esta revisão os dois escritores engoliam `OSError`/`IOError` em nível
+debug, então nenhum chamador distinguia commit durável de escrita perdida. Um
+refresh podia gastar o único refresh token, reportar sucesso e deixar no disco
+o par já consumido — que um restart re-semeava, fazendo o refresh seguinte
+reenviar um token gasto.
+
+O que mudou:
+
+- `_write_claude_code_credentials()` / `_write_hermes_oauth_credentials()`
+ levantam `CredentialPersistError` em vez de engolir o erro.
+- `_refresh_oauth_token()` trata commit falho como refresh falho.
+- `_refresh_entry_impl()` falha fechado nos caminhos primário e de retry: a
+ rotação nunca é marcada, persistida ou devolvida, e a entrada vai para
+ quarentena `DEAD` com razão `credential_persist_failed`, virando um pedido
+ explícito de reautenticação em vez de um fallback silencioso para outro
+ provedor. O retry commita no singleton antes de persistir a linha do pool.
+- `_upsert_entry()` deixou de tratar a re-semeadura de uma fonte *borrowed*
+ como rotação de chave (compara `secret_fingerprint`), senão cada load
+ limpava o `DEAD` recém-escrito e ressuscitava a credencial em quarentena.
+
+Cobertura: `tests/agent/test_anthropic_credential_persist_failure.py` força a
+falha do escritor a partir de cada ponto de entrada e prova, em todos, que um
+`load_pool()` posterior não devolve o par pré-refresh como credencial usável.
+
+`agent/anthropic_adapter.py` foi dividido na mesma revisão (3.423 → 1.215
+linhas) em `agent/anthropic_endpoints.py`, `agent/anthropic_message_convert.py`
+e `agent/anthropic_credentials.py`, para que a superfície de credencial tenha
+um único dono. O adapter re-exporta todos os nomes, então os imports existentes
+continuam resolvendo.
+
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.py
index d96754483d0..91e67241de4 100644
--- a/agent/anthropic_adapter.py
+++ b/agent/anthropic_adapter.py
@@ -26,6 +26,90 @@ from typing import Any, Dict, List, Optional, Tuple
from utils import base_url_host_matches, base_url_hostname, normalize_proxy_env_vars
from agent.secret_scope import get_secret as _get_secret
+# This module keeps client construction and the Messages API call itself. The
+# three surfaces it used to inline now live next to it:
+#
+# agent/anthropic_endpoints.py base-URL/endpoint-family predicates
+# agent/anthropic_message_convert.py OpenAI -> Anthropic payload conversion
+# agent/anthropic_credentials.py credential sources, OAuth, refresh commit
+#
+# All three are re-exported below so long standing
+# ``from agent.anthropic_adapter import resolve_anthropic_token`` (or
+# ``convert_messages_to_anthropic``, ...) imports keep resolving.
+from agent.anthropic_endpoints import ( # noqa: F401
+ _KIMI_FAMILY_EXACT_SLUGS,
+ _KIMI_FAMILY_MODEL_PREFIXES,
+ _base_url_needs_context_1m_beta,
+ _is_azure_anthropic_endpoint,
+ _is_deepseek_anthropic_endpoint,
+ _is_kimi_coding_endpoint,
+ _is_kimi_family_endpoint,
+ _is_minimax_anthropic_endpoint,
+ _is_nous_portal_endpoint,
+ _is_opencode_endpoint,
+ _is_third_party_anthropic_endpoint,
+ _model_name_is_kimi_family,
+ _normalize_base_url_text,
+ _requires_bearer_auth,
+)
+from agent.anthropic_message_convert import ( # noqa: F401
+ _EMPTY_TEXT_PLACEHOLDER,
+ _apply_assistant_cache_control_to_last_cacheable_block,
+ _content_parts_to_anthropic_blocks,
+ _convert_assistant_message,
+ _convert_content_part_to_anthropic,
+ _convert_content_to_anthropic,
+ _convert_tool_message_to_result,
+ _convert_user_message,
+ _ensure_leading_user_turn,
+ _evict_old_screenshots,
+ _extract_preserved_thinking_blocks,
+ _fix_blank_text_blocks_in_list,
+ _image_source_from_openai_url,
+ _is_bedrock_model_id,
+ _manage_thinking_signatures,
+ _merge_consecutive_roles,
+ _normalize_tool_input_schema,
+ _safe_text,
+ _sanitize_replay_block,
+ _sanitize_tool_id,
+ _scrub_blank_text_blocks,
+ _strip_orphaned_tool_blocks,
+ _to_plain_data,
+ convert_messages_to_anthropic,
+ convert_tools_to_anthropic,
+ normalize_model_name,
+)
+from agent.anthropic_credentials import ( # noqa: F401
+ _OAUTH_CLIENT_ID,
+ _OAUTH_REDIRECT_URI,
+ _OAUTH_SCOPES,
+ _OAUTH_TOKEN_URL,
+ _OAUTH_TOKEN_URLS,
+ _OAUTH_TOKEN_USER_AGENT,
+ CredentialPersistError,
+ _generate_pkce,
+ _get_hermes_oauth_file,
+ _getenv,
+ _is_oauth_token,
+ _prefer_refreshable_claude_code_token,
+ _read_claude_code_credentials_from_file,
+ _read_claude_code_credentials_from_keychain,
+ _refresh_oauth_token,
+ _resolve_anthropic_pool_token,
+ _resolve_claude_code_token_from_credentials,
+ _write_claude_code_credentials,
+ _write_hermes_oauth_credentials,
+ claude_code_credentials_path,
+ is_claude_code_token_valid,
+ read_claude_code_credentials,
+ read_hermes_oauth_credentials,
+ refresh_anthropic_oauth_pure,
+ resolve_anthropic_token,
+ run_hermes_oauth_login_pure,
+ run_oauth_setup_token,
+)
+
try:
import hermes_cli as _hermes_cli
@@ -34,15 +118,6 @@ except Exception:
_HERMES_VERSION = "0.0.0"
-def _getenv(name: str, default: str = "") -> str:
- """Profile-scoped replacement for os.getenv on credential reads.
-
- Routes through the secret scope (Workstream A): identical to os.getenv
- when multiplexing is off, scope-aware (and fail-closed on an unscoped
- read) when on. Mirrors the same wrapper in hermes_cli/runtime_provider.py.
- """
- val = _get_secret(name, default)
- return val if val is not None else default
# NOTE: `import anthropic` is deliberately NOT at module top — the SDK pulls
# ~220 ms of imports (anthropic.types, anthropic.lib.tools._beta_runner, etc.)
@@ -448,271 +523,7 @@ def _get_claude_code_version() -> str:
return _claude_code_version_cache
-def _is_oauth_token(key: str) -> bool:
- """Check if the key is an Anthropic OAuth/setup token.
-
- Positively identifies Anthropic OAuth tokens by their key format:
- - ``sk-ant-`` prefix (but NOT ``sk-ant-api``) → setup tokens, managed keys
- - ``eyJ`` prefix → JWTs from the Anthropic OAuth flow
- - ``cc-`` prefix → Claude Code OAuth access tokens (from CLAUDE_CODE_OAUTH_TOKEN)
-
- Non-Anthropic keys (MiniMax, Alibaba, etc.) don't match any pattern
- and correctly return False.
- """
- if not key:
- return False
- # Regular Anthropic Console API keys — x-api-key auth, never OAuth
- if key.startswith("sk-ant-api"):
- return False
- # Anthropic-issued tokens (setup-tokens sk-ant-oat-*, managed keys)
- if key.startswith("sk-ant-"):
- return True
- # JWTs from Anthropic OAuth flow
- if key.startswith("eyJ"):
- return True
- # Claude Code OAuth access tokens (opaque, from CLAUDE_CODE_OAUTH_TOKEN)
- if key.startswith("cc-"):
- return True
- return False
-
-
-def _normalize_base_url_text(base_url) -> str:
- """Normalize SDK/base transport URL values to a plain string for inspection.
-
- Some client objects expose ``base_url`` as an ``httpx.URL`` instead of a raw
- string. Provider/auth detection should accept either shape.
- """
- if not base_url:
- return ""
- return str(base_url).strip()
-
-
-def _is_third_party_anthropic_endpoint(base_url: str | None) -> bool:
- """Return True for non-Anthropic endpoints using the Anthropic Messages API.
-
- Third-party proxies (Microsoft Foundry, AWS Bedrock, self-hosted) authenticate
- with their own API keys via x-api-key, not Anthropic OAuth tokens. OAuth
- detection should be skipped for these endpoints.
- """
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False # No base_url = direct Anthropic API
- normalized = normalized.rstrip("/").lower()
- if "anthropic.com" in normalized:
- return False # Direct Anthropic API — OAuth applies
- return True # Any other endpoint is a third-party proxy
-
-
-def _is_kimi_coding_endpoint(base_url: str | None) -> bool:
- """Return True for Kimi's /coding endpoint that requires claude-code UA."""
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False
- return normalized.rstrip("/").lower().startswith("https://api.kimi.com/coding")
-
-
-def _is_opencode_endpoint(base_url: str | None) -> bool:
- """Return True for OpenCode's Zen/Go relay (opencode.ai)."""
- return base_url_host_matches(base_url or "", "opencode.ai")
-
-
-# Model-name prefixes that identify the Kimi / Moonshot family. Covers
-# - official slugs: ``kimi-k2.5``, ``kimi_thinking``, ``moonshot-v1-8k``
-# - common release lines: ``k1.5-...``, ``k2-thinking``, ``k25-...``, ``k2.5-...``,
-# and the bare Coding Plan slug ``k3`` (plus ``k3.x``/``k3-...`` variants)
-# Matched case-insensitively against the post-``normalize_model_name`` form,
-# so a caller's ``provider/vendor/model`` slug is handled the same as a
-# bare name.
-_KIMI_FAMILY_MODEL_PREFIXES = (
- "kimi-", "kimi_",
- "moonshot-", "moonshot_",
- "k1.", "k1-",
- "k2.", "k2-",
- "k25", "k2.5",
- "k3.", "k3-",
-)
-
-# Bare release slugs with no separator suffix (Kimi Coding Plan serves K3
-# as the exact slug ``k3``). Kept exact-match so unrelated model names that
-# merely start with the same characters don't get misclassified.
-_KIMI_FAMILY_EXACT_SLUGS = frozenset({"k3"})
-
-
-def _model_name_is_kimi_family(model: str | None) -> bool:
- if not isinstance(model, str):
- return False
- m = model.strip().lower()
- if not m:
- return False
- # Strip vendor prefix (e.g. ``moonshotai/kimi-k2.5`` → ``kimi-k2.5``)
- if "/" in m:
- m = m.rsplit("/", 1)[-1]
- if m in _KIMI_FAMILY_EXACT_SLUGS:
- return True
- return m.startswith(_KIMI_FAMILY_MODEL_PREFIXES)
-
-
-def _is_kimi_family_endpoint(base_url: str | None, model: str | None = None) -> bool:
- """Return True for any Kimi / Moonshot Anthropic-Messages-speaking endpoint.
-
- Broader than ``_is_kimi_coding_endpoint`` — matches:
-
- - Kimi's official ``/coding`` URL (legacy check, preserved)
- - Any ``api.kimi.com`` / ``moonshot.ai`` / ``moonshot.cn`` host
- - Custom or proxied endpoints whose *model* name is in the Kimi / Moonshot
- family (``kimi-*``, ``moonshot-*``, ``k1.*``, ``k2.*``, …). Users with
- ``api_mode: anthropic_messages`` on a private gateway fronting Kimi
- fall into this branch — the upstream still enforces Kimi's thinking
- semantics (reasoning_content required on every replayed tool-call
- message) regardless of the gateway's hostname.
-
- Used to decide whether to drop Anthropic's ``thinking`` kwarg and to
- preserve unsigned reasoning_content-derived thinking blocks on replay.
- See hermes-agent#13848, #17057.
- """
- if _is_kimi_coding_endpoint(base_url):
- return True
- for _domain in ("api.kimi.com", "moonshot.ai", "moonshot.cn"):
- if base_url_host_matches(base_url or "", _domain):
- return True
- if _model_name_is_kimi_family(model):
- return True
- return False
-
-
-def _is_deepseek_anthropic_endpoint(base_url: str | None) -> bool:
- """Return True for DeepSeek's Anthropic-compatible endpoint.
-
- DeepSeek's ``/anthropic`` route speaks the Anthropic Messages protocol
- but, when thinking mode is enabled, requires the ``thinking`` blocks
- from prior assistant turns to round-trip on subsequent requests — the
- generic third-party path strips them and triggers HTTP 400::
-
- The content[].thinking in the thinking mode must be passed back
- to the API.
-
- Per DeepSeek's published compatibility matrix the blocks are unsigned
- (no Anthropic-proprietary signature, no ``redacted_thinking`` support),
- so this endpoint is handled with the same strip-signed / keep-unsigned
- policy used for Kimi's ``/coding`` endpoint. The match is pinned to
- the ``/anthropic`` path so the OpenAI-compatible ``api.deepseek.com``
- base URL (which never reaches this adapter) is not misclassified.
- See hermes-agent#16748.
- """
- if not base_url_host_matches(base_url or "", "api.deepseek.com"):
- return False
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False
- return "/anthropic" in normalized.rstrip("/").lower()
-
-
-def _is_nous_portal_endpoint(base_url: str | None) -> bool:
- """Return True for Nous Portal's Anthropic Messages route.
-
- Portal serves its ``anthropic/*`` catalog natively at
- ``https://inference-api.nousresearch.com/v1/messages``. Portal-specific
- behaviours key off this: Bearer JWT auth, verbatim catalog model ids,
- and native thinking-signature replay.
-
- Trusted hosts only:
-
- 1. Prod hostname ``inference-api.nousresearch.com``
- 2. The operator-set ``NOUS_INFERENCE_BASE_URL`` hostname (staging/preview)
-
- Lookalikes such as ``inference-api.nousresearch.com.attacker.test`` are
- rejected (hostname match, not substring).
- """
- if base_url_host_matches(base_url or "", "inference-api.nousresearch.com"):
- return True
- try:
- from hermes_cli.auth import _nous_inference_env_override
-
- override = _nous_inference_env_override()
- except Exception:
- return False
- if not override:
- return False
- # Exact host equality (not subdomain) so the env override can't broaden
- # into sibling hosts the operator did not set.
- override_host = base_url_hostname(override)
- return bool(override_host) and base_url_hostname(base_url or "") == override_host
-
-def _requires_bearer_auth(base_url: str | None) -> bool:
- """Return True for Anthropic-compatible providers that require Bearer auth.
-
- Some third-party /anthropic endpoints implement Anthropic's Messages API but
- require Authorization: Bearer instead of Anthropic's native x-api-key header.
- MiniMax's global and China Anthropic-compatible endpoints, Azure AI
- Foundry's Anthropic-style endpoint, Palantir Foundry's LLM proxy, and Nous
- Portal's Messages route follow this pattern.
- """
- if _is_nous_portal_endpoint(base_url):
- return True
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False
- normalized = normalized.rstrip("/").lower()
- return (
- normalized.startswith(("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic"))
- or "azure.com" in normalized
- # Palantir Foundry LLM proxy (<org>.palantirfoundry.com/api/v2/llm/proxy/anthropic)
- # rejects x-api-key with 401 and requires Authorization: Bearer.
- # Hostname match (not substring) so e.g. evil.com/palantirfoundry
- # paths don't trigger Bearer auth.
- or base_url_host_matches(normalized, "palantirfoundry.com")
- # CommandCode's /provider/v1/messages endpoint uses Bearer auth,
- # not Anthropic's native x-api-key header. Hostname match for the
- # same reason as above.
- or base_url_host_matches(normalized, "api.commandcode.ai")
- )
-
-
-def _base_url_needs_context_1m_beta(base_url: str | None) -> bool:
- """Return True for endpoints that still gate 1M context behind a beta."""
- normalized = _normalize_base_url_text(base_url).lower()
- if not normalized:
- return False
- return "azure.com" in normalized
-
-
-def _is_minimax_anthropic_endpoint(base_url: str | None) -> bool:
- """Return True for MiniMax's Anthropic-compatible endpoints.
-
- MiniMax rejects the fine-grained-tool-streaming and context-1m betas;
- those need to be stripped even though MiniMax also uses Bearer auth.
- """
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False
- normalized = normalized.rstrip("/").lower()
- return normalized.startswith(
- ("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic")
- )
-
-
-def _is_azure_anthropic_endpoint(base_url: str | None) -> bool:
- """Return True for Azure-hosted Anthropic Messages endpoints.
-
- Covers both the modern Foundry host family (``*.services.ai.azure.*``)
- and the legacy Azure OpenAI host family (``*.openai.azure.*``) when
- serving Anthropic's ``/anthropic`` route. Used to opt-in those hosts
- to the ``api-version`` query-param plumbing required by Azure.
-
- Intentionally avoids a finite allow-list of TLD suffixes so it works
- across sovereign / private Azure clouds.
- """
- normalized = _normalize_base_url_text(base_url)
- if not normalized:
- return False
- parsed = urlparse(normalized)
- host = (parsed.hostname or "").lower().rstrip(".")
- path = (parsed.path or "").lower()
- host_padded = f".{host}."
- is_foundry_host = ".services.ai.azure." in host_padded
- is_legacy_azoai_host = ".openai.azure." in host_padded
- return (is_foundry_host or is_legacy_azoai_host) and "/anthropic" in path
def _common_betas_for_base_url(
@@ -1022,2025 +833,6 @@ def build_anthropic_bedrock_client(region: str):
)
-class CredentialPersistError(RuntimeError):
- """A rotated single-use credential could not be durably committed.
-
- Anthropic OAuth refresh tokens are single-use: a successful refresh POST
- consumes the old refresh token server-side and returns a replacement. The
- replacement exists only in memory until it reaches its authoritative
- on-disk store (``~/.claude/.credentials.json`` for ``claude_code``,
- ``~/.hermes/.anthropic_oauth.json`` for ``hermes_pkce``).
-
- If that write fails and the caller reports success anyway, the on-disk
- (already consumed) pair survives and is re-seeded on the next
- ``load_pool()``, so the following refresh replays a spent token and fails
- with ``invalid_grant`` / ``refresh_token_reused``. Callers must therefore
- treat this as a failed refresh, not a successful one, and fail closed.
- """
-
- def __init__(self, path: Any, cause: BaseException) -> None:
- super().__init__(
- f"failed to durably persist rotated Anthropic credentials to {path}: {cause}"
- )
- self.path = path
-
-
-def _read_claude_code_credentials_from_keychain() -> Optional[Dict[str, Any]]:
- """Read Claude Code OAuth credentials from the macOS Keychain.
-
- Claude Code >=2.1.114 stores credentials in the macOS Keychain under the
- service name "Claude Code-credentials" rather than (or in addition to)
- the JSON file at ~/.claude/.credentials.json.
-
- The password field contains a JSON string with the same claudeAiOauth
- structure as the JSON file.
-
- Returns dict with {accessToken, refreshToken?, expiresAt?} or None.
- """
- if platform.system() != "Darwin":
- return None
-
- try:
- # Read the "Claude Code-credentials" generic password entry
- result = subprocess.run(
- ["security", "find-generic-password",
- "-s", "Claude Code-credentials",
- "-w"],
- capture_output=True,
- text=True, encoding='utf-8', errors='replace',
- timeout=5,
- stdin=subprocess.DEVNULL,
- )
- except (OSError, subprocess.TimeoutExpired):
- logger.debug("Keychain: security command not available or timed out")
- return None
-
- if result.returncode != 0:
- logger.debug("Keychain: no entry found for 'Claude Code-credentials'")
- return None
-
- raw = result.stdout.strip()
- if not raw:
- return None
-
- try:
- data = json.loads(raw)
- except json.JSONDecodeError:
- logger.debug("Keychain: credentials payload is not valid JSON")
- return None
-
- oauth_data = data.get("claudeAiOauth")
- if oauth_data and isinstance(oauth_data, dict):
- access_token = oauth_data.get("accessToken", "")
- if access_token:
- return {
- "accessToken": access_token,
- "refreshToken": oauth_data.get("refreshToken", ""),
- "expiresAt": oauth_data.get("expiresAt", 0),
- "source": "macos_keychain",
- }
-
- return None
-
-
-def claude_code_credentials_path() -> Path:
- """Location Claude Code CLI writes its shared OAuth credentials file.
-
- This file is not profile-owned: every Hermes profile's credential pool
- reads and writes the *same* path, so cross-profile refresh races on a
- ``claude_code`` pool entry must be serialized against this exact path
- (see ``CredentialPool._claude_code_credentials_lock`` in
- ``agent/credential_pool.py``).
- """
- return Path.home() / ".claude" / ".credentials.json"
-
-
-def _read_claude_code_credentials_from_file() -> Optional[Dict[str, Any]]:
- """Read Claude Code OAuth credentials from ~/.claude/.credentials.json.
-
- Returns dict with {accessToken, refreshToken?, expiresAt?, source} or None.
- """
- cred_path = claude_code_credentials_path()
- if not cred_path.exists():
- return None
- try:
- data = json.loads(cred_path.read_text(encoding="utf-8"))
- except (json.JSONDecodeError, OSError, IOError) as e:
- logger.debug("Failed to read ~/.claude/.credentials.json: %s", e)
- return None
-
- oauth_data = data.get("claudeAiOauth")
- if not (oauth_data and isinstance(oauth_data, dict)):
- return None
- access_token = oauth_data.get("accessToken", "")
- if not access_token:
- return None
- return {
- "accessToken": access_token,
- "refreshToken": oauth_data.get("refreshToken", ""),
- "expiresAt": oauth_data.get("expiresAt", 0),
- "source": "claude_code_credentials_file",
- }
-
-
-def read_claude_code_credentials() -> Optional[Dict[str, Any]]:
- """Read refreshable Claude Code OAuth credentials.
-
- Reads from two possible sources and reconciles them:
- 1. macOS Keychain (Darwin only) — "Claude Code-credentials" entry
- 2. ~/.claude/.credentials.json file
-
- Selection rules when both are present:
- - If exactly one is non-expired, prefer that one. (Handles the case
- where Claude Code refreshes one source but not the other — observed
- in the wild on Claude Code 2.1.x.)
- - Otherwise, prefer the source with the later ``expiresAt`` so that
- any subsequent refresh uses the most recent ``refreshToken``.
-
- This intentionally excludes ~/.claude.json primaryApiKey. Opencode's
- subscription flow is OAuth/setup-token based with refreshable credentials,
- and native direct Anthropic provider usage should follow that path rather
- than auto-detecting Claude's first-party managed key.
-
- Returns dict with {accessToken, refreshToken?, expiresAt?, source} or None.
- """
- kc_creds = _read_claude_code_credentials_from_keychain()
- file_creds = _read_claude_code_credentials_from_file()
-
- if kc_creds and file_creds:
- kc_valid = is_claude_code_token_valid(kc_creds)
- file_valid = is_claude_code_token_valid(file_creds)
- if kc_valid and not file_valid:
- return kc_creds
- if file_valid and not kc_valid:
- return file_creds
- # Both valid or both expired: prefer the later expiresAt so the
- # downstream refresh path uses the freshest refresh_token.
- kc_exp = kc_creds.get("expiresAt", 0) or 0
- file_exp = file_creds.get("expiresAt", 0) or 0
- return kc_creds if kc_exp >= file_exp else file_creds
-
- return kc_creds or file_creds
-
-
-def is_claude_code_token_valid(creds: Dict[str, Any]) -> bool:
- """Check if Claude Code credentials have a non-expired access token."""
- import time
-
- expires_at = creds.get("expiresAt", 0)
- if not expires_at:
- # No expiry set (managed keys) — valid if token is present
- return bool(creds.get("accessToken"))
-
- # expiresAt is in milliseconds since epoch
- now_ms = int(time.time() * 1000)
- # Allow 60 seconds of buffer
- return now_ms < (expires_at - 60_000)
-
-
-def refresh_anthropic_oauth_pure(refresh_token: str, *, use_json: bool = False) -> Dict[str, Any]:
- """Refresh an Anthropic OAuth token without mutating local credential files."""
- import time
- import urllib.parse
- import urllib.request
-
- if not refresh_token:
- raise ValueError("refresh_token is required")
-
- client_id = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
- if use_json:
- data = json.dumps({
- "grant_type": "refresh_token",
- "refresh_token": refresh_token,
- "client_id": client_id,
- }).encode()
- content_type = "application/json"
- else:
- data = urllib.parse.urlencode({
- "grant_type": "refresh_token",
- "refresh_token": refresh_token,
- "client_id": client_id,
- }).encode()
- content_type = "application/x-www-form-urlencoded"
-
- token_endpoints = [
- "https://platform.claude.com/v1/oauth/token",
- "https://console.anthropic.com/v1/oauth/token",
- ]
- last_error = None
- for endpoint in token_endpoints:
- req = urllib.request.Request(
- endpoint,
- data=data,
- headers={
- "Content-Type": content_type,
- "User-Agent": _OAUTH_TOKEN_USER_AGENT,
- },
- method="POST",
- )
- try:
- with urllib.request.urlopen(req, timeout=10) as resp:
- result = json.loads(resp.read().decode())
- except Exception as exc:
- last_error = exc
- logger.debug("Anthropic token refresh failed at %s: %s", endpoint, exc)
- continue
-
- access_token = result.get("access_token", "")
- if not access_token:
- raise ValueError("Anthropic refresh response was missing access_token")
- next_refresh = result.get("refresh_token", refresh_token)
- expires_in = result.get("expires_in", 3600)
- return {
- "access_token": access_token,
- "refresh_token": next_refresh,
- "expires_at_ms": int(time.time() * 1000) + (expires_in * 1000),
- }
-
- if last_error is not None:
- raise last_error
- raise ValueError("Anthropic token refresh failed")
-
-
-def _refresh_oauth_token(creds: Dict[str, Any]) -> Optional[str]:
- """Attempt to refresh an expired Claude Code OAuth token.
-
- Claude Code's OAuth refresh tokens are single-use: a successful refresh
- rotates the pair and invalidates the old refresh token. Claude Code itself
- also refreshes on its own schedule (IDE/CLI activity), so by the time
- Hermes notices an expired token, Claude Code may have already rotated it.
- POSTing our now-stale refresh token in that window races Claude Code and
- fails with ``invalid_grant``.
-
- So before refreshing, re-read the live credential sources. If Claude Code
- has already produced a valid token, adopt it and skip the POST entirely.
- Only fall back to refreshing ourselves when no fresh credential is found.
- """
- # Claude Code may have already refreshed — adopt its token rather than
- # racing it with our (possibly already-rotated) refresh token. The read,
- # decision, POST, and write-back all belong to the shared credentials
- # source, so hold the same path-keyed cross-process lock used by the pool.
- # Without this direct resolver path, two profiles can still spend one
- # single-use refresh token even though CredentialPool is serialized.
- try:
- from hermes_cli.auth import AUTH_LOCK_TIMEOUT_SECONDS, _auth_store_lock, env_float
-
- refresh_timeout_seconds = env_float(
- "HERMES_ANTHROPIC_REFRESH_TIMEOUT_SECONDS", 20
- )
- lock_timeout_seconds = max(
- float(AUTH_LOCK_TIMEOUT_SECONDS),
- float(refresh_timeout_seconds) + 5.0,
- )
- with _auth_store_lock(
- timeout_seconds=lock_timeout_seconds,
- target_path=claude_code_credentials_path(),
- ):
- # Only adopt when the live re-read produced a DIFFERENT token with
- # a real future expiry: re-adopting the same credential we were
- # just handed would be a no-op, and a 0/absent ``expiresAt`` means
- # "managed key / unknown expiry" (see is_claude_code_token_valid).
- current = read_claude_code_credentials()
- if current:
- current_token = current.get("accessToken", "")
- current_exp = current.get("expiresAt", 0) or 0
- if (
- current_token
- and current_token != creds.get("accessToken", "")
- and current_exp > 0
- and is_claude_code_token_valid(current)
- ):
- logger.debug("Adopted Claude Code's already-refreshed OAuth token")
- return current_token
-
- refresh_token = (
- (current or {}).get("refreshToken", "")
- or creds.get("refreshToken", "")
- )
- if not refresh_token:
- logger.debug("No refresh token available — cannot refresh")
- return None
-
- try:
- refreshed = refresh_anthropic_oauth_pure(refresh_token, use_json=False)
- except Exception as e:
- logger.debug("Failed to refresh Claude Code token: %s", e)
- return None
-
- # The POST above already consumed ``refresh_token`` server-side.
- # Writing the replacement pair is the commit step of that
- # transaction, not a cache update: if it fails, the rotation is
- # unrecoverable and the pair still on disk is spent. Fail closed
- # rather than handing back an access token whose refresh half was
- # lost — reporting success here is what lets a later load replay
- # the consumed token and produce ``invalid_grant``.
- try:
- _write_claude_code_credentials(
- refreshed["access_token"],
- refreshed["refresh_token"],
- refreshed["expires_at_ms"],
- )
- except Exception as e:
- logger.error(
- "Anthropic OAuth refresh rotated the single-use token but could not "
- "commit it to %s (%s) — treating the refresh as failed; "
- "re-run 'claude setup-token' to reauthenticate",
- claude_code_credentials_path(),
- e,
- )
- return None
-
- logger.debug("Successfully refreshed Claude Code OAuth token")
- return refreshed["access_token"]
- except Exception as e:
- # Lock acquisition/read failures should preserve the resolver's
- # existing fail-soft contract rather than taking down agent startup.
- logger.debug("Failed to acquire Claude Code refresh lock: %s", e)
- return None
-
-
-def _write_claude_code_credentials(
- access_token: str,
- refresh_token: str,
- expires_at_ms: int,
- *,
- scopes: Optional[list] = None,
-) -> None:
- """Write refreshed credentials back to ~/.claude/.credentials.json.
-
- The optional *scopes* list (e.g. ``["user:inference", "user:profile", ...]``)
- is persisted so that Claude Code's own auth check recognises the credential
- as valid. Claude Code >=2.1.81 gates on the presence of ``"user:inference"``
- in the stored scopes before it will use the token.
-
- Raises ``CredentialPersistError`` when the rotated pair does not reach the
- file. This write is the commit step of the refresh transaction, not a
- best-effort cache update: a swallowed failure leaves the consumed
- pre-rotation pair on disk to be re-seeded and replayed (see
- ``CredentialPersistError``).
- """
- cred_path = claude_code_credentials_path()
- try:
- # Read existing file to preserve other fields
- existing = {}
- if cred_path.exists():
- existing = json.loads(cred_path.read_text(encoding="utf-8"))
-
- oauth_data: Dict[str, Any] = {
- "accessToken": access_token,
- "refreshToken": refresh_token,
- "expiresAt": expires_at_ms,
- }
- if scopes is not None:
- oauth_data["scopes"] = scopes
- elif "claudeAiOauth" in existing and "scopes" in existing["claudeAiOauth"]:
- # Preserve previously-stored scopes when the refresh response
- # does not include a scope field.
- oauth_data["scopes"] = existing["claudeAiOauth"]["scopes"]
-
- existing["claudeAiOauth"] = oauth_data
-
- cred_path.parent.mkdir(parents=True, exist_ok=True)
- # Per-process random suffix avoids collisions between concurrent
- # writers and stale leftovers from a prior crashed write.
- _tmp_cred = cred_path.with_suffix(f".tmp.{os.getpid()}.{secrets.token_hex(4)}")
- try:
- # Create the temp file atomically at 0o600. The previous
- # write_text + post-replace chmod opened a TOCTOU window where
- # both the temp file and the destination briefly inherited the
- # process umask (commonly 0o644 = world-readable), exposing
- # Claude Code OAuth tokens to other local users between create
- # and chmod. Mirrors agent/google_oauth.py (#19673) and
- # tools/mcp_oauth.py (#21148). Parent dir (~/.claude/) is
- # owned by Claude Code itself, so we leave its mode alone.
- fd = os.open(
- str(_tmp_cred),
- os.O_WRONLY | os.O_CREAT | os.O_EXCL,
- stat.S_IRUSR | stat.S_IWUSR,
- )
- with os.fdopen(fd, "w", encoding="utf-8") as fh:
- json.dump(existing, fh, indent=2)
- fh.flush()
- os.fsync(fh.fileno())
- os.replace(_tmp_cred, cred_path)
- except OSError:
- try:
- _tmp_cred.unlink(missing_ok=True)
- except OSError:
- pass
- raise
- except (OSError, IOError, ValueError) as e:
- # ValueError covers a corrupt existing file (JSONDecodeError): the
- # merge-read is part of the commit, so failing it means the rotated
- # pair never landed either.
- logger.error("Failed to write refreshed credentials to %s: %s", cred_path, e)
- raise CredentialPersistError(cred_path, e) from e
-
-
-def _resolve_claude_code_token_from_credentials(creds: Optional[Dict[str, Any]] = None) -> Optional[str]:
- """Resolve a token from Claude Code credential files, refreshing if needed."""
- creds = creds or read_claude_code_credentials()
- if creds and is_claude_code_token_valid(creds):
- logger.debug("Using Claude Code credentials (auto-detected)")
- return creds["accessToken"]
- if creds:
- logger.debug("Claude Code credentials expired — attempting refresh")
- refreshed = _refresh_oauth_token(creds)
- if refreshed:
- return refreshed
- logger.debug("Token refresh failed — re-run 'claude setup-token' to reauthenticate")
- return None
-
-
-def _prefer_refreshable_claude_code_token(env_token: str, creds: Optional[Dict[str, Any]]) -> Optional[str]:
- """Prefer Claude Code creds when a persisted env OAuth token would shadow refresh.
-
- Hermes historically persisted setup tokens into ANTHROPIC_TOKEN. That makes
- later refresh impossible because the static env token wins before we ever
- inspect Claude Code's refreshable credential file. If we have a refreshable
- Claude Code credential record, prefer it over the static env OAuth token.
- """
- if not env_token or not _is_oauth_token(env_token) or not isinstance(creds, dict):
- return None
- if not creds.get("refreshToken"):
- return None
-
- resolved = _resolve_claude_code_token_from_credentials(creds)
- if resolved and resolved != env_token:
- logger.debug(
- "Preferring Claude Code credential file over static env OAuth token so refresh can proceed"
- )
- return resolved
- return None
-
-
-def _resolve_anthropic_pool_token() -> Optional[str]:
- """Return the first available Anthropic OAuth token from credential_pool.
-
- Read-only: enumerates with ``clear_expired=False, refresh=False`` so a bare
- token *resolve* (which runs from diagnostic/read-only call sites such as
- ``account_usage`` and ``hermes models``) never mutates ``~/.hermes/auth.json``
- or makes a network refresh call. Refresh-on-expiry is owned by the API call
- path's pool recovery, not the resolver.
- """
- try:
- from agent.credential_pool import AUTH_TYPE_OAUTH, load_pool
- except Exception:
- return None
-
- try:
- pool = load_pool("anthropic")
- # Enumerate read-only (clear_expired=False, refresh=False): never persist
- # to auth.json or trigger a network refresh from a bare resolve. select()
- # is deliberately NOT used — it runs clear_expired=True, refresh=True,
- # which would violate this read-only contract.
- entries, _pending = pool._available_entries(clear_expired=False, refresh=False)
- except Exception:
- logger.debug("Failed to read Anthropic credential_pool", exc_info=True)
- return None
-
- for entry in entries:
- if getattr(entry, "auth_type", None) != AUTH_TYPE_OAUTH:
- continue
- # access_token is a declared field but a persisted entry can carry an
- # explicit null (or a partially-written OAuth entry), so coerce before
- # strip — a bare None.strip() here would escape the try/excepts above
- # and crash the whole resolver, taking down the source #5 fallback too.
- # Matches the aux-client analog (auxiliary_client.py: str(key or "")).
- token = (getattr(entry, "access_token", None) or "").strip()
- if token:
- return token
-
- return None
-
-
-def resolve_anthropic_token() -> Optional[str]:
- """Resolve an Anthropic token from all available sources.
-
- Priority:
- 1. ANTHROPIC_TOKEN env var (OAuth/setup token saved by Hermes)
- 2. CLAUDE_CODE_OAUTH_TOKEN env var
- 3. ANTHROPIC_API_KEY env var (explicit regular API key)
- 4. Claude Code credentials (~/.claude.json or ~/.claude/.credentials.json)
- — with automatic refresh if expired and a refresh token is available
- 5. Anthropic credential_pool OAuth entry (~/.hermes/auth.json)
-
- Returns the token string or None.
- """
- creds: Optional[Dict[str, Any]] = None
- creds_loaded = False
-
- def _read_creds() -> Optional[Dict[str, Any]]:
- nonlocal creds, creds_loaded
- if not creds_loaded:
- creds = read_claude_code_credentials()
- creds_loaded = True
- return creds
-
- # 1. Hermes-managed OAuth/setup token env var
- token = _getenv("ANTHROPIC_TOKEN").strip()
- if token:
- preferred = _prefer_refreshable_claude_code_token(token, _read_creds())
- if preferred:
- return preferred
- return token
-
- # 2. CLAUDE_CODE_OAUTH_TOKEN (used by Claude Code for setup-tokens)
- cc_token = _getenv("CLAUDE_CODE_OAUTH_TOKEN").strip()
- if cc_token:
- preferred = _prefer_refreshable_claude_code_token(cc_token, _read_creds())
- if preferred:
- return preferred
- return cc_token
-
- # 3. Regular API key. An explicit user-configured key must not be shadowed
- # by auto-discovered Claude Code or credential-pool OAuth credentials.
- api_key = _getenv("ANTHROPIC_API_KEY").strip()
- if api_key:
- return api_key
-
- # 4. Claude Code credential file
- resolved_claude_token = _resolve_claude_code_token_from_credentials(_read_creds())
- if resolved_claude_token:
- return resolved_claude_token
-
- # 5. Hermes credential_pool OAuth entry.
- resolved_pool_token = _resolve_anthropic_pool_token()
- if resolved_pool_token:
- return resolved_pool_token
-
- return None
-
-
-def run_oauth_setup_token() -> Optional[str]:
- """Run 'claude setup-token' interactively and return the resulting token.
-
- Checks multiple sources after the subprocess completes:
- 1. Claude Code credential files (may be written by the subprocess)
- 2. CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_TOKEN env vars
-
- Returns the token string, or None if no credentials were obtained.
- Raises FileNotFoundError if the 'claude' CLI is not installed.
- """
- import shutil
- import subprocess
-
- claude_path = shutil.which("claude")
- if not claude_path:
- raise FileNotFoundError(
- "The 'claude' CLI is not installed. "
- "Install it with: npm install -g @anthropic-ai/claude-code"
- )
-
- # Run interactively — stdin/stdout/stderr inherited so the user can
- # complete the OAuth login prompt. Must keep inherited stdin; the TUI-EOF
- # concern does not apply to an interactive login the user explicitly
- # invokes. noqa: subprocess-stdin
- try:
- subprocess.run([claude_path, "setup-token"])
- except (KeyboardInterrupt, EOFError):
- return None
-
- # Check if credentials were saved to Claude Code's config files
- creds = read_claude_code_credentials()
- if creds and is_claude_code_token_valid(creds):
- return creds["accessToken"]
-
- # Check env vars that may have been set
- for env_var in ("CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_TOKEN"):
- val = _getenv(env_var).strip()
- if val:
- return val
-
- return None
-
-
-# ── Hermes-native PKCE OAuth flow ────────────────────────────────────────
-# Mirrors the flow used by Claude Code, pi-ai, and OpenCode.
-# Stores credentials in ~/.hermes/.anthropic_oauth.json (our own file).
-
-_OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
-# Anthropic migrated the OAuth token endpoint to platform.claude.com;
-# console.anthropic.com now 404s. Callers should iterate _OAUTH_TOKEN_URLS
-# (new host first, console fallback). _OAUTH_TOKEN_URL is kept as the primary
-# for backward compatibility with existing imports and now points at the live host.
-_OAUTH_TOKEN_URLS = [
- "https://platform.claude.com/v1/oauth/token",
- "https://console.anthropic.com/v1/oauth/token",
-]
-_OAUTH_TOKEN_URL = _OAUTH_TOKEN_URLS[0]
-# User-Agent sent on the OAuth *token endpoint* (login exchange + refresh).
-# Anthropic rate-limits (HTTP 429) any token-endpoint request whose UA starts
-# with ``claude-code/`` — verified empirically against platform.claude.com:
-# ``claude-code/2.1.200`` and ``Mozilla/5.0`` -> 429; ``axios/*``, ``node``,
-# and SDK-style UAs -> 400 (reached code validation). The real Claude Code CLI
-# exchanges the auth code with a bare axios client (``axios/<ver>``), NOT its
-# ``claude-code/`` inference UA. We mirror that here. NOTE: the *inference* path
-# (build_anthropic_kwargs) still uses the ``claude-code/`` UA + ``x-app: cli`` —
-# that fingerprint is required there and is NOT throttled on the messages API.
-_OAUTH_TOKEN_USER_AGENT = "axios/1.7.9"
-_OAUTH_REDIRECT_URI = "https://console.anthropic.com/oauth/code/callback"
-_OAUTH_SCOPES = "org:create_api_key user:profile user:inference"
-def _get_hermes_oauth_file() -> Path:
- return get_hermes_home() / ".anthropic_oauth.json"
-
-
-def _generate_pkce() -> tuple:
- """Generate PKCE code_verifier and code_challenge (S256)."""
- import base64
- import hashlib
- import secrets
-
- verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
- challenge = base64.urlsafe_b64encode(
- hashlib.sha256(verifier.encode()).digest()
- ).rstrip(b"=").decode()
- return verifier, challenge
-
-
-def run_hermes_oauth_login_pure() -> Optional[Dict[str, Any]]:
- """Run Hermes-native OAuth PKCE flow and return credential state."""
- import secrets
- import time
- import webbrowser
-
- verifier, challenge = _generate_pkce()
- oauth_state = secrets.token_urlsafe(32)
-
- params = {
- "code": "true",
- "client_id": _OAUTH_CLIENT_ID,
- "response_type": "code",
- "redirect_uri": _OAUTH_REDIRECT_URI,
- "scope": _OAUTH_SCOPES,
- "code_challenge": challenge,
- "code_challenge_method": "S256",
- "state": oauth_state,
- }
- from urllib.parse import urlencode
-
- auth_url = f"https://claude.ai/oauth/authorize?{urlencode(params)}"
-
- print()
- print("Authorize Hermes with your Claude Pro/Max subscription.")
- print()
- print("╭─ Claude Pro/Max Authorization ────────────────────╮")
- print("│ │")
- print("│ Open this link in your browser: │")
- print("╰───────────────────────────────────────────────────╯")
- print()
- print(f" {auth_url}")
- print()
-
- try:
- from hermes_cli.auth import _can_open_graphical_browser as _can_open_gui
- except Exception:
- _can_open_gui = lambda: True # noqa: E731 — degrade to prior behavior
-
- if _can_open_gui():
- try:
- webbrowser.open(auth_url)
- print(" (Browser opened automatically)")
- except Exception:
- pass
-
- print()
- print("After authorizing, you'll see a code. Paste it below.")
- print()
- try:
- auth_code = input("Authorization code: ").strip()
- except (KeyboardInterrupt, EOFError):
- return None
-
- if not auth_code:
- print("No code entered.")
- return None
-
- splits = auth_code.split("#")
- code = splits[0]
- received_state = splits[1] if len(splits) > 1 else ""
-
- # Validate state to prevent CSRF (RFC 6749 §10.12)
- if received_state != oauth_state:
- logger.warning("OAuth state mismatch — possible CSRF, aborting")
- return None
-
- try:
- import urllib.request
-
- exchange_data = json.dumps({
- "grant_type": "authorization_code",
- "client_id": _OAUTH_CLIENT_ID,
- "code": code,
- "state": received_state,
- "redirect_uri": _OAUTH_REDIRECT_URI,
- "code_verifier": verifier,
- }).encode()
-
- # Anthropic migrated the OAuth token endpoint to platform.claude.com;
- # console.anthropic.com now 404s. Try the new host first, then fall
- # back to console for older deployments (mirrors the refresh path).
- # UA is _OAUTH_TOKEN_USER_AGENT (a non-claude-code UA) — see the
- # constant's definition for why the token endpoint must not send
- # claude-code/ (429 UA-prefix block).
- result = None
- last_error = None
- for endpoint in _OAUTH_TOKEN_URLS:
- req = urllib.request.Request(
- endpoint,
- data=exchange_data,
- headers={
- "Content-Type": "application/json",
- "User-Agent": _OAUTH_TOKEN_USER_AGENT,
- },
- method="POST",
- )
- try:
- with urllib.request.urlopen(req, timeout=15) as resp:
- result = json.loads(resp.read().decode())
- break
- except Exception as exc:
- last_error = exc
- logger.debug("Anthropic token exchange failed at %s: %s", endpoint, exc)
- continue
-
- if result is None:
- raise last_error if last_error is not None else ValueError(
- "Anthropic token exchange failed"
- )
- except Exception as e:
- print(f"Token exchange failed: {e}")
- return None
-
- access_token = result.get("access_token", "")
- refresh_token = result.get("refresh_token", "")
- expires_in = result.get("expires_in", 3600)
-
- if not access_token:
- print("No access token in response.")
- return None
-
- expires_at_ms = int(time.time() * 1000) + (expires_in * 1000)
- return {
- "access_token": access_token,
- "refresh_token": refresh_token,
- "expires_at_ms": expires_at_ms,
- }
-
-
-def read_hermes_oauth_credentials() -> Optional[Dict[str, Any]]:
- """Read Hermes-managed OAuth credentials from ~/.hermes/.anthropic_oauth.json."""
- oauth_file = _get_hermes_oauth_file()
- if oauth_file.exists():
- try:
- data = json.loads(oauth_file.read_text(encoding="utf-8"))
- if data.get("accessToken"):
- return data
- except (json.JSONDecodeError, OSError, IOError) as e:
- logger.debug("Failed to read Hermes OAuth credentials: %s", e)
- return None
-
-
-def _write_hermes_oauth_credentials(
- access_token: str,
- refresh_token: Optional[str],
- expires_at_ms: Optional[int],
-) -> None:
- """Write refreshed hermes_pkce tokens back to ~/.hermes/.anthropic_oauth.json.
-
- Without this, a successful pool-level refresh of a ``hermes_pkce``-sourced
- entry is invisible to this singleton file. The next ``load_pool()`` call
- runs ``_seed_from_singletons()``, which reads the stale file and
- overwrites the freshly-rotated pool entry with the pre-refresh (and, for
- single-use Anthropic refresh tokens, already-consumed) token pair.
-
- Raises ``CredentialPersistError`` when the rotated pair does not reach the
- file, for the same reason ``_write_claude_code_credentials`` does: this is
- the commit step of the refresh transaction.
- """
- oauth_file = _get_hermes_oauth_file()
- try:
- oauth_data = {
- "accessToken": access_token,
- "refreshToken": refresh_token,
- "expiresAt": expires_at_ms,
- }
- oauth_file.parent.mkdir(parents=True, exist_ok=True)
- _tmp_oauth = oauth_file.with_suffix(f".tmp.{os.getpid()}.{secrets.token_hex(4)}")
- try:
- fd = os.open(
- str(_tmp_oauth),
- os.O_WRONLY | os.O_CREAT | os.O_EXCL,
- stat.S_IRUSR | stat.S_IWUSR,
- )
- with os.fdopen(fd, "w", encoding="utf-8") as fh:
- json.dump(oauth_data, fh, indent=2)
- fh.flush()
- os.fsync(fh.fileno())
- os.replace(_tmp_oauth, oauth_file)
- except OSError:
- try:
- _tmp_oauth.unlink(missing_ok=True)
- except OSError:
- pass
- raise
- except (OSError, IOError, ValueError) as e:
- logger.error(
- "Failed to write refreshed Hermes OAuth credentials to %s: %s", oauth_file, e
- )
- raise CredentialPersistError(oauth_file, e) from e
-
-
-# ---------------------------------------------------------------------------
-# Message / tool / response format conversion
-# ---------------------------------------------------------------------------
-
-
-def _is_bedrock_model_id(model: str) -> bool:
- """Detect AWS Bedrock model IDs that use dots as namespace separators.
-
- Bedrock model IDs come in two forms:
- - Bare: ``anthropic.claude-opus-4-7``
- - Regional (inference profiles): ``us.anthropic.claude-sonnet-4-5-v1:0``
-
- In both cases the dots separate namespace components, not version
- numbers, and must be preserved verbatim for the Bedrock API.
- """
- lower = model.lower()
- # Regional inference-profile prefixes
- if any(lower.startswith(p) for p in (
- "global.", "us.", "eu.", "apac.", "ap.", "au.", "jp.",
- "ca.", "sa.", "me.", "af.",
- )):
- return True
- # Bare Bedrock model IDs: provider.model-family
- if lower.startswith("anthropic."):
- return True
- return False
-
-
-def normalize_model_name(model: str, preserve_dots: bool = False) -> str:
- """Normalize a model name for the Anthropic API.
-
- - Strips 'anthropic/' prefix (OpenRouter format, case-insensitive)
- - Converts dots to hyphens in version numbers (OpenRouter uses dots,
- Anthropic uses hyphens: claude-opus-4.6 → claude-opus-4-6), unless
- preserve_dots is True (e.g. for Alibaba/DashScope: qwen3.5-plus).
- - Preserves Bedrock model IDs (``anthropic.claude-opus-4-7``) and
- regional inference profiles (``us.anthropic.claude-*``) whose dots
- are namespace separators, not version separators.
- """
- lower = model.lower()
- if lower.startswith("anthropic/"):
- model = model[len("anthropic/"):]
- if not preserve_dots:
- # Bedrock model IDs use dots as namespace separators
- # (e.g. "anthropic.claude-opus-4-7", "us.anthropic.claude-*").
- # These must not be converted to hyphens. See issue #12295.
- if _is_bedrock_model_id(model):
- return model
- # Only convert dots to hyphens for Anthropic/Claude models.
- # Non-Anthropic models (gpt-5.4, gemini-2.5, etc.) use dots
- # as part of their canonical names. See issue #17171.
- _lower = model.lower()
- if _lower.startswith("claude-") or _lower.startswith("anthropic/"):
- model = model.replace(".", "-")
- return model
-
-
-def _sanitize_tool_id(tool_id: str) -> str:
- """Sanitize a tool call ID for the Anthropic API.
-
- Anthropic requires IDs matching [a-zA-Z0-9_-]. Replace invalid
- characters with underscores and ensure non-empty.
- """
- import re
- if not tool_id:
- return "tool_0"
- sanitized = re.sub(r"[^a-zA-Z0-9_-]", "_", tool_id)
- return sanitized or "tool_0"
-
-
-def _normalize_tool_input_schema(schema: Any) -> Dict[str, Any]:
- """Normalize tool schemas before sending them to Anthropic.
-
- Anthropic's tool schema validator rejects nullable unions such as
- ``anyOf: [{"type": "string"}, {"type": "null"}]`` that Pydantic/MCP
- commonly emits for optional fields. Tool optionality is represented by
- the parent ``required`` array, so we delegate to the shared
- ``strip_nullable_unions`` helper to collapse nullable unions to the
- non-null branch while preserving metadata like description/default.
-
- ``keep_nullable_hint=False`` because the Anthropic validator does not
- recognize the OpenAPI-style ``nullable: true`` extension and strict
- schema-to-grammar converters may reject unknown keywords.
-
- Top-level ``oneOf``/``allOf``/``anyOf`` are also stripped here: the
- Anthropic API rejects union keywords at the schema root with a generic
- HTTP 400. Several upstream and plugin tools ship schemas with one of
- these keywords at the top level (commonly for Pydantic discriminated
- unions). If we land here with those keywords still present after
- nullable-union stripping, drop them and fall back to a plain object
- schema so the tool still validates at the Anthropic boundary.
- """
- if not schema:
- return {"type": "object", "properties": {}}
-
- from tools.schema_sanitizer import strip_nullable_unions
-
- normalized = strip_nullable_unions(schema, keep_nullable_hint=False)
- if not isinstance(normalized, dict):
- return {"type": "object", "properties": {}}
- # Strip top-level union keywords that Anthropic's validator rejects.
- banned = {"oneOf", "allOf", "anyOf"}
- if banned & normalized.keys():
- normalized = {k: v for k, v in normalized.items() if k not in banned}
- if "type" not in normalized:
- normalized["type"] = "object"
- if normalized.get("type") == "object" and not isinstance(normalized.get("properties"), dict):
- normalized = {**normalized, "properties": {}}
- return normalized
-
-
-def convert_tools_to_anthropic(tools: List[Dict]) -> List[Dict]:
- """Convert OpenAI tool definitions to Anthropic format."""
- if not tools:
- return []
- result = []
- seen_names: set = set()
- for t in tools:
- fn = t.get("function", {})
- name = fn.get("name", "")
- # Defensive dedup: Anthropic rejects requests with duplicate tool
- # names. Upstream injection paths already dedup, but this guard
- # converts a hard API failure into a warning. See: #18478
- if name and name in seen_names:
- logger.warning(
- "convert_tools_to_anthropic: duplicate tool name '%s' "
- "— dropping second occurrence",
- name,
- )
- continue
- if name:
- seen_names.add(name)
- anthropic_tool: Dict[str, Any] = {
- "name": name,
- "description": fn.get("description", ""),
- "input_schema": _normalize_tool_input_schema(
- fn.get("parameters", {"type": "object", "properties": {}})
- ),
- }
- # Forward cache_control marker when present on the OpenAI-format
- # tool dict. Anthropic's tools array supports cache_control on the
- # last tool to cache the entire schema cross-session.
- cache_control = t.get("cache_control")
- if isinstance(cache_control, dict):
- anthropic_tool["cache_control"] = dict(cache_control)
- result.append(anthropic_tool)
- return result
-
-
-def _image_source_from_openai_url(url: str) -> Dict[str, str]:
- """Convert an OpenAI-style image URL/data URL into Anthropic image source."""
- url = str(url or "").strip()
- if not url:
- return {"type": "url", "url": ""}
-
- if url.startswith("data:"):
- header, _, data = url.partition(",")
- media_type = "image/jpeg"
- if header.startswith("data:"):
- mime_part = header[len("data:"):].split(";", 1)[0].strip()
- if mime_part.startswith("image/"):
- media_type = mime_part
- return {
- "type": "base64",
- "media_type": media_type,
- "data": data,
- }
-
- return {"type": "url", "url": url}
-
-
-def _convert_content_part_to_anthropic(part: Any) -> Optional[Dict[str, Any]]:
- """Convert a single OpenAI-style content part to Anthropic format."""
- if part is None:
- return None
- if isinstance(part, str):
- return {"type": "text", "text": part}
- if not isinstance(part, dict):
- return {"type": "text", "text": str(part)}
-
- ptype = part.get("type")
-
- if ptype == "input_text":
- block: Dict[str, Any] = {"type": "text", "text": part.get("text", "")}
- elif ptype == "text":
- # A stored Anthropic text block. Rebuild from whitelisted fields only —
- # SDK response text blocks carry output-only siblings (parsed_output,
- # citations=None) that the Messages INPUT schema rejects with HTTP 400
- # "Extra inputs are not permitted". Do NOT dict(part) it verbatim.
- block = {"type": "text", "text": part.get("text", "")}
- cits = part.get("citations")
- if isinstance(cits, list) and cits:
- block["citations"] = cits
- elif ptype in {"image_url", "input_image"}:
- image_value = part.get("image_url", {})
- url = image_value.get("url", "") if isinstance(image_value, dict) else str(image_value or "")
- block = {"type": "image", "source": _image_source_from_openai_url(url)}
- else:
- block = dict(part)
-
- if isinstance(part.get("cache_control"), dict) and "cache_control" not in block:
- block["cache_control"] = dict(part["cache_control"])
- return block
-
-
-def _to_plain_data(value: Any, *, _depth: int = 0, _path: Optional[set] = None) -> Any:
- """Recursively convert SDK objects to plain Python data structures.
-
- Guards against circular references (``_path`` tracks ``id()`` of objects
- on the *current* recursion path) and runaway depth (capped at 20 levels).
- Uses path-based tracking so shared (but non-cyclic) objects referenced by
- multiple siblings are converted correctly rather than being stringified.
- """
- _MAX_DEPTH = 20
- if _depth > _MAX_DEPTH:
- return str(value)
-
- if _path is None:
- _path = set()
-
- obj_id = id(value)
- if obj_id in _path:
- return str(value)
-
- if hasattr(value, "model_dump"):
- _path.add(obj_id)
- try:
- # warnings=False: content blocks from the streaming accumulator
- # (ParsedTextBlock et al.) trip pydantic's serializer-mismatch
- # UserWarning against the generic Message union; the dump itself
- # is correct, and the warning leaks to the user's terminal.
- dumped = value.model_dump(warnings=False)
- except TypeError:
- # Duck-typed model_dump without pydantic's signature.
- dumped = value.model_dump()
- result = _to_plain_data(dumped, _depth=_depth + 1, _path=_path)
- _path.discard(obj_id)
- return result
- if isinstance(value, dict):
- _path.add(obj_id)
- result = {k: _to_plain_data(v, _depth=_depth + 1, _path=_path) for k, v in value.items()}
- _path.discard(obj_id)
- return result
- if isinstance(value, (list, tuple)):
- _path.add(obj_id)
- result = [_to_plain_data(v, _depth=_depth + 1, _path=_path) for v in value]
- _path.discard(obj_id)
- return result
- if hasattr(value, "__dict__"):
- _path.add(obj_id)
- result = {
- k: _to_plain_data(v, _depth=_depth + 1, _path=_path)
- for k, v in vars(value).items()
- if not k.startswith("_")
- }
- _path.discard(obj_id)
- return result
- return value
-
-
-def _extract_preserved_thinking_blocks(message: Dict[str, Any]) -> List[Dict[str, Any]]:
- """Return Anthropic thinking blocks previously preserved on the message."""
- raw_details = message.get("reasoning_details")
- if not isinstance(raw_details, list):
- return []
-
- preserved: List[Dict[str, Any]] = []
- for detail in raw_details:
- if not isinstance(detail, dict):
- continue
- block_type = str(detail.get("type", "") or "").strip().lower()
- if block_type not in {"thinking", "redacted_thinking"}:
- continue
- preserved.append(copy.deepcopy(detail))
- return preserved
-
-
-def _convert_content_to_anthropic(content: Any) -> Any:
- """Convert OpenAI-style multimodal content arrays to Anthropic blocks."""
- if not isinstance(content, list):
- return content
-
- converted = []
- for part in content:
- block = _convert_content_part_to_anthropic(part)
- if block is not None:
- converted.append(block)
- return converted
-
-
-def _content_parts_to_anthropic_blocks(parts: Any) -> List[Dict[str, Any]]:
- """Convert OpenAI-style tool-message content parts → Anthropic tool_result inner blocks.
-
- Used for multimodal tool results (e.g. computer_use screenshots). Each
- part is normalized via `_convert_content_part_to_anthropic`, then
- filtered to the block types Anthropic tool_result accepts (text + image).
- """
- if not isinstance(parts, list):
- return []
- out: List[Dict[str, Any]] = []
- for part in parts:
- block = _convert_content_part_to_anthropic(part)
- if not block:
- continue
- btype = block.get("type")
- if btype == "text":
- text_val = block.get("text")
- if isinstance(text_val, str) and text_val:
- out.append({"type": "text", "text": text_val})
- elif btype == "image":
- src = block.get("source")
- if isinstance(src, dict) and src:
- out.append({"type": "image", "source": src})
- return out
-
-
-_EMPTY_TEXT_PLACEHOLDER = "(empty)"
-
-
-def _safe_text(text: Any) -> str:
- """Return ``text`` if it's non-whitespace, else a non-whitespace placeholder.
-
- The Anthropic Messages API rejects requests where a text content block is
- empty or whitespace-only (HTTP 400 "text content blocks must contain
- non-whitespace text"). When such a block gets stored in session history —
- e.g. produced by context compression — it is replayed verbatim on every
- subsequent turn, permanently wedging the session. Coercing to a
- non-whitespace placeholder is self-healing: the next API call recovers.
-
- Mirrors ``bedrock_adapter._safe_text`` (#9486); ref #69512.
- """
- if text is None:
- return _EMPTY_TEXT_PLACEHOLDER
- if not isinstance(text, str):
- text = str(text)
- return text if text.strip() else _EMPTY_TEXT_PLACEHOLDER
-
-
-def _sanitize_replay_block(b: Dict[str, Any]) -> Optional[Dict[str, Any]]:
- """Strip output-only fields from a stored Anthropic content block so it is
- valid as REQUEST input on replay.
-
- The SDK response objects carry output-only attributes that the Messages
- *input* schema forbids ("Extra inputs are not permitted"): text blocks get
- ``parsed_output``/``citations`` (when null), tool_use blocks get ``caller``,
- etc. ``normalize_response`` captured blocks verbatim via ``_to_plain_data``,
- so these leak back as input on the next turn → HTTP 400.
-
- Whitelist per type (NOT a blacklist) so future SDK output-only fields can't
- reintroduce the bug. Returns a clean block, or None to drop it.
- """
- if not isinstance(b, dict):
- return None
- btype = b.get("type")
- if btype == "text":
- text_val = b.get("text", "")
- # Bedrock and strict Anthropic-compatible endpoints reject text
- # blocks where "text" is empty or whitespace-only (#69512). Drop the
- # blank block (the caller relocates any cache_control it carried and
- # falls back to a non-whitespace placeholder when nothing survives)
- # rather than coercing in place — a coerced "(empty)" block would be
- # model-visible noise next to surviving thinking/tool_use blocks.
- # Type-safe: captured blocks can carry text=None from an invalid
- # upstream payload, which a bare .strip() would crash on.
- if not isinstance(text_val, str) or not text_val.strip():
- return None
- out: Dict[str, Any] = {"type": "text", "text": text_val}
- # citations is input-valid ONLY when it's a non-empty list; the SDK
- # emits citations=None on responses, which the input schema rejects.
- cits = b.get("citations")
- if isinstance(cits, list) and cits:
- out["citations"] = cits
- if isinstance(b.get("cache_control"), dict):
- out["cache_control"] = b["cache_control"]
- return out
- if btype == "thinking":
- out = {"type": "thinking", "thinking": b.get("thinking", "")}
- if b.get("signature"):
- out["signature"] = b["signature"]
- return out
- if btype == "redacted_thinking":
- # Only valid with its data payload; drop if missing.
- return {"type": "redacted_thinking", "data": b["data"]} if b.get("data") else None
- if btype == "tool_use":
- out = {
- "type": "tool_use",
- "id": _sanitize_tool_id(b.get("id", "")),
- "name": b.get("name", ""),
- "input": b.get("input", {}),
- }
- if isinstance(b.get("cache_control"), dict):
- out["cache_control"] = b["cache_control"]
- return out
- if btype == "image":
- src = b.get("source")
- return {"type": "image", "source": src} if isinstance(src, dict) else None
- # Unknown/unsupported block type on the input path — drop rather than risk
- # another "Extra inputs are not permitted".
- return None
-
-
-def _apply_assistant_cache_control_to_last_cacheable_block(
- blocks: List[Dict[str, Any]],
- cache_control: Any,
-) -> None:
- if not isinstance(cache_control, dict):
- return
- for block in reversed(blocks):
- if isinstance(block, dict) and block.get("type") in {"text", "tool_use"}:
- block.setdefault("cache_control", dict(cache_control))
- break
-
-
-def _convert_assistant_message(m: Dict[str, Any]) -> Dict[str, Any]:
- """Convert an assistant message to Anthropic content blocks.
-
- Handles thinking blocks, regular content, tool calls, and
- reasoning_content injection for Kimi/DeepSeek endpoints.
- """
- content = m.get("content", "")
- # Anthropic interleaved-thinking fast path: when this turn carries a
- # verbatim, order-preserving block list (set by normalize_response only
- # for turns that interleave SIGNED thinking with tool_use), replay it.
- # Each block is run through _sanitize_replay_block to strip output-only
- # SDK fields (parsed_output, caller, citations=None, …) that the Messages
- # INPUT schema forbids — replaying them verbatim caused HTTP 400 "Extra
- # inputs are not permitted" (text.parsed_output). Block ORDER is preserved
- # (the reason this channel exists); only forbidden sibling fields are
- # dropped, leaving thinking signatures and tool_use id/name/input intact.
- ordered_blocks = m.get("anthropic_content_blocks")
- if isinstance(ordered_blocks, list) and ordered_blocks:
- # Re-source each tool_use input from the stored tool_calls map rather
- # than the captured block. The ordered-blocks list captures tool_use
- # input from the RAW API response (normalize_response), which is NOT
- # credential-redacted; tool_calls[].function.arguments IS redacted at
- # storage time (build_assistant_message, #19798). Replaying the raw
- # block input would resurrect a secret the model inlined into a tool
- # call (e.g. terminal(command="curl -H 'Authorization: Bearer sk-...'")
- # onto the wire, even though the same value is redacted everywhere else
- # in history. Keying by sanitized tool id preserves interleave order
- # (the reason this channel exists) while swapping in the redacted
- # input. Adapted from #36071 (replay-time tool-input re-sourcing).
- redacted_input_by_id: Dict[str, Any] = {}
- for tc in m.get("tool_calls", []) or []:
- if not isinstance(tc, dict):
- continue
- fn = tc.get("function", {}) or {}
- raw_args = fn.get("arguments", "{}")
- try:
- parsed_args = json.loads(raw_args) if isinstance(raw_args, str) else raw_args
- except (json.JSONDecodeError, ValueError):
- parsed_args = {}
- redacted_input_by_id[_sanitize_tool_id(tc.get("id", ""))] = parsed_args
- replayed: List[Dict[str, Any]] = []
- _relocated_replay_cache_control = None
- _dropped_blank_text = False
- for b in ordered_blocks:
- clean = _sanitize_replay_block(b)
- if clean is None:
- if isinstance(b, dict) and b.get("type") == "text":
- _dropped_blank_text = True
- if isinstance(b, dict) and isinstance(b.get("cache_control"), dict):
- # A dropped blank text block can still carry the cache
- # breakpoint marker -- relocate it rather than losing it.
- _relocated_replay_cache_control = b["cache_control"]
- continue
- if clean.get("type") == "tool_use":
- # Override raw (un-redacted) input with the redacted copy when
- # we have one for this id; fall back to the sanitized block
- # input only if the tool_call is missing (shape mismatch).
- redacted = redacted_input_by_id.get(clean.get("id", ""))
- if redacted is not None:
- clean["input"] = redacted
- replayed.append(clean)
- # When every text block was blank and nothing cacheable survived
- # (e.g. signed thinking + a blank text block, or a SOLE blank
- # cache-marked block), emit the non-whitespace placeholder so the
- # replayed message stays schema-valid (#69512) and a relocated cache
- # marker still has a carrier instead of being silently lost.
- _has_cacheable_replay = any(
- isinstance(b, dict) and b.get("type") in {"text", "tool_use"}
- for b in replayed
- )
- if not _has_cacheable_replay and (
- _dropped_blank_text or _relocated_replay_cache_control is not None
- ):
- replayed.append({"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER})
- if replayed:
- if _relocated_replay_cache_control is not None:
- _apply_assistant_cache_control_to_last_cacheable_block(
- replayed, _relocated_replay_cache_control
- )
- _apply_assistant_cache_control_to_last_cacheable_block(
- replayed, m.get("cache_control")
- )
- # apply_anthropic_cache_control marks an assistant turn with
- # non-empty text by writing cache_control INTO ``content`` (see
- # _apply_cache_marker's list branch), not at the top level. This
- # branch rebuilds the message from ordered_blocks and never reads
- # ``content``, so that marker would be dropped -- and because
- # _can_carry_marker already counted this message as a carrier, the
- # breakpoint is burned rather than relocated. #56195 covered the
- # complementary shape (blank content -> top-level marker); this is
- # the interleaved thinking + preamble-text + tool_use shape.
- _inline_cc = None
- _msg_content = m.get("content")
- if isinstance(_msg_content, list):
- for _blk in _msg_content:
- if isinstance(_blk, dict) and isinstance(
- _blk.get("cache_control"), dict
- ):
- _inline_cc = _blk["cache_control"]
- break
- if _inline_cc is not None:
- _apply_assistant_cache_control_to_last_cacheable_block(
- replayed, _inline_cc
- )
- return {"role": "assistant", "content": replayed}
-
- blocks = _extract_preserved_thinking_blocks(m)
- # Cache markers dropped along with a blank block are relocated onto the
- # last surviving cacheable block below (via
- # _apply_assistant_cache_control_to_last_cacheable_block), rather than
- # lost -- prompt_caching.py's _apply_cache_marker() sets cache_control
- # directly on content[-1] for list content, so if that last part happens
- # to be blank text, dropping it silently would lose the breakpoint.
- _relocated_cache_control = None
- if content:
- if isinstance(content, list):
- converted_content = _convert_content_to_anthropic(content)
- if isinstance(converted_content, list):
- # Bedrock and strict Anthropic-compatible endpoints reject
- # text blocks where "text" is empty or whitespace-only. The
- # ordered-replay path enforces the same invariant via
- # _sanitize_replay_block(). Type-safe against ANY invalid
- # "text" value from an upstream payload -- None, or a
- # truthy non-string like an int -- not just None: checking
- # isinstance() first (rather than `blk.get("text") or ""`)
- # means a non-string value is treated as blank/invalid
- # instead of reaching .strip() and raising AttributeError.
- for blk in converted_content:
- _blk_text = blk.get("text") if isinstance(blk, dict) else None
- if (
- isinstance(blk, dict)
- and blk.get("type") == "text"
- and (not isinstance(_blk_text, str) or not _blk_text.strip())
- ):
- if isinstance(blk.get("cache_control"), dict):
- _relocated_cache_control = blk["cache_control"]
- continue
- blocks.append(blk)
- else:
- # Scalar (non-list) content: a whitespace-only string is the
- # same invalid-payload case as an empty list block -- drop it
- # rather than emitting a blank text block.
- text_str = str(content)
- if text_str.strip():
- blocks.append({"type": "text", "text": text_str})
- for tc in m.get("tool_calls", []):
- if not tc or not isinstance(tc, dict):
- continue
- fn = tc.get("function", {})
- args = fn.get("arguments", "{}")
- try:
- parsed_args = json.loads(args) if isinstance(args, str) else args
- except (json.JSONDecodeError, ValueError):
- parsed_args = {}
- blocks.append({
- "type": "tool_use",
- "id": _sanitize_tool_id(tc.get("id", "")),
- "name": fn.get("name", ""),
- "input": parsed_args,
- })
- # Kimi's /coding endpoint (Anthropic protocol) requires assistant
- # tool-call messages to carry reasoning_content when thinking is
- # enabled server-side. Preserve it as a thinking block so Kimi
- # can validate the message history. See hermes-agent#13848.
- #
- # Accept empty string "" — _copy_reasoning_content_for_api()
- # injects "" as a tier-3 fallback for Kimi tool-call messages
- # that had no reasoning. Kimi requires the field to exist, even
- # if empty.
- #
- # Prepend (not append): Anthropic protocol requires thinking
- # blocks before text and tool_use blocks.
- #
- # Guard: only add when reasoning_details didn't already contribute
- # thinking blocks. On native Anthropic, reasoning_details produces
- # signed thinking blocks — adding another unsigned one from
- # reasoning_content would create a duplicate (same text) that gets
- # downgraded to a spurious text block on the last assistant message.
- reasoning_content = m.get("reasoning_content")
- _already_has_thinking = any(
- isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"}
- for b in blocks
- )
- if isinstance(reasoning_content, str) and not _already_has_thinking:
- blocks.insert(0, {"type": "thinking", "thinking": reasoning_content})
- # Anthropic rejects empty assistant content. IMPORTANT: fall back only
- # to the placeholder, never to the raw `content` variable -- `content`
- # is the UNFILTERED original message content, and can itself be exactly
- # the blank/whitespace-only payload the filtering above just removed
- # (a sole blank text block, or scalar whitespace with no tool_calls).
- # `blocks or content` there would silently restore the invalid provider
- # payload this function exists to prevent (#69512).
- effective = blocks if blocks else [{"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER}]
- # Applied here (after the empty-fallback resolution) rather than
- # earlier against `blocks` directly, so a cache_control relocated from
- # a dropped blank block that was the ONLY block still lands on the
- # (empty) placeholder instead of being silently lost when blocks was
- # empty at the point the marker would otherwise have been applied.
- if _relocated_cache_control is not None:
- _apply_assistant_cache_control_to_last_cacheable_block(
- effective, _relocated_cache_control
- )
- _apply_assistant_cache_control_to_last_cacheable_block(
- effective, m.get("cache_control")
- )
- return {"role": "assistant", "content": effective}
-
-
-def _convert_tool_message_to_result(
- result: List[Dict[str, Any]], m: Dict[str, Any]
-) -> None:
- """Convert a tool message to an Anthropic tool_result, merging consecutive
- results into one user message.
-
- Mutates ``result`` in place — either appends a new user message or extends
- the trailing user message's tool_result list.
- """
- content = m.get("content", "")
- multimodal_blocks: Optional[List[Dict[str, Any]]] = None
- if isinstance(content, dict) and content.get("_multimodal"):
- multimodal_blocks = _content_parts_to_anthropic_blocks(
- content.get("content") or []
- )
- # Fallback text if the conversion produced nothing usable.
- if not multimodal_blocks and content.get("text_summary"):
- multimodal_blocks = [
- {"type": "text", "text": str(content["text_summary"])}
- ]
- elif isinstance(content, list):
- converted = _content_parts_to_anthropic_blocks(content)
- if any(b.get("type") == "image" for b in converted):
- multimodal_blocks = converted
- # Back-compat: some callers stash blocks under a private key.
- if multimodal_blocks is None:
- stashed = m.get("_anthropic_content_blocks")
- if isinstance(stashed, list) and stashed:
- text_content = content if isinstance(content, str) and content.strip() else None
- multimodal_blocks = (
- [{"type": "text", "text": text_content}] + stashed
- if text_content else list(stashed)
- )
-
- if multimodal_blocks:
- result_content: Any = multimodal_blocks
- elif isinstance(content, str):
- result_content = content
- else:
- result_content = json.dumps(content) if content else "(no output)"
- if not result_content:
- result_content = "(no output)"
- tool_result = {
- "type": "tool_result",
- "tool_use_id": _sanitize_tool_id(m.get("tool_call_id", "")),
- "content": result_content,
- }
- if isinstance(m.get("cache_control"), dict):
- tool_result["cache_control"] = dict(m["cache_control"])
- # Merge consecutive tool results into one user message
- if (
- result
- and result[-1]["role"] == "user"
- and isinstance(result[-1]["content"], list)
- and result[-1]["content"]
- and result[-1]["content"][0].get("type") == "tool_result"
- ):
- result[-1]["content"].append(tool_result)
- else:
- result.append({"role": "user", "content": [tool_result]})
-
-
-def _convert_user_message(content: Any) -> Dict[str, Any]:
- """Validate and convert a user message to anthropic format."""
- if isinstance(content, list):
- converted_blocks = _convert_content_to_anthropic(content)
- kept_blocks = _fix_blank_text_blocks_in_list(
- converted_blocks,
- placeholder_text="(empty message)",
- msg_index=-1,
- role="user",
- location="_convert_user_message",
- )
- return {"role": "user", "content": kept_blocks}
- else:
- if not content or (isinstance(content, str) and not content.strip()):
- content = "(empty message)"
- return {"role": "user", "content": content}
-
-
-def _strip_orphaned_tool_blocks(result: List[Dict[str, Any]]) -> None:
- """Strip tool_use blocks with no matching tool_result, and vice versa.
-
- Context compression or session truncation can remove either side of a
- tool-call pair, or insert messages between a tool_use and its result.
- Anthropic requires each tool_use to have a matching tool_result in the
- IMMEDIATELY FOLLOWING user message — a global ID match is not enough.
- Mutates ``result`` in place.
- """
- # Pass 1: For each assistant message with tool_use blocks, check that
- # EACH tool_use ID has a matching tool_result in the immediately following
- # user message. Strip tool_use blocks that lack an adjacent result —
- # Anthropic rejects non-adjacent pairs with HTTP 400 even when the IDs
- # match somewhere later in the conversation.
- for i, m in enumerate(result):
- if m.get("role") != "assistant" or not isinstance(m.get("content"), list):
- continue
- tool_use_ids_in_turn = {
- b.get("id")
- for b in m["content"]
- if isinstance(b, dict) and b.get("type") == "tool_use"
- }
- if not tool_use_ids_in_turn:
- continue
-
- # Collect result IDs from the immediately following user message only.
- adjacent_result_ids: set = set()
- if i + 1 < len(result):
- nxt = result[i + 1]
- if nxt.get("role") == "user" and isinstance(nxt.get("content"), list):
- for block in nxt["content"]:
- if isinstance(block, dict) and block.get("type") == "tool_result":
- adjacent_result_ids.add(block.get("tool_use_id"))
-
- orphaned = tool_use_ids_in_turn - adjacent_result_ids
- if not orphaned:
- continue
-
- kept = [
- b
- for b in m["content"]
- if not (isinstance(b, dict) and b.get("type") == "tool_use" and b.get("id") in orphaned)
- ]
- # If stripping an orphaned tool_use mutated a turn that also carries a
- # signed thinking block, that block's Anthropic signature was computed
- # against the ORIGINAL (un-stripped) turn content and is now invalid.
- # Anthropic rejects the replayed turn with HTTP 400 "thinking blocks in
- # the latest assistant message cannot be modified". Flag the turn so
- # _manage_thinking_signatures can demote the dead signature instead of
- # replaying it verbatim. See hermes-agent: extended-thinking + parallel
- # tool batch interrupted mid-flight → non-retryable 400 crash-loop.
- if len(kept) != len(m["content"]) and any(
- isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"}
- for b in m["content"]
- ):
- m["_thinking_signature_invalidated"] = True
- m["content"] = kept if kept else [{"type": "text", "text": "(tool call removed)"}]
-
- # Pass 2: Rebuild the set of tool_use IDs that survived pass 1, then
- # strip tool_result blocks that no longer have any matching tool_use
- # anywhere in the conversation.
- surviving_tool_use_ids: set = set()
- for m in result:
- if m.get("role") == "assistant" and isinstance(m.get("content"), list):
- for block in m["content"]:
- if isinstance(block, dict) and block.get("type") == "tool_use":
- surviving_tool_use_ids.add(block.get("id"))
-
- for m in result:
- if m.get("role") != "user" or not isinstance(m.get("content"), list):
- continue
- new_content = [
- b
- for b in m["content"]
- if not (isinstance(b, dict) and b.get("type") == "tool_result")
- or b.get("tool_use_id") in surviving_tool_use_ids
- ]
- if len(new_content) != len(m["content"]):
- m["content"] = new_content if new_content else [{"type": "text", "text": "(tool result removed)"}]
-
-
-def _merge_consecutive_roles(result: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
- """Merge consecutive same-role messages to enforce Anthropic alternation.
-
- Returns a new list (caller must rebind ``result``).
- """
- fixed = []
- for m in result:
- if fixed and fixed[-1]["role"] == m["role"]:
- if m["role"] == "user":
- prev_content = fixed[-1]["content"]
- curr_content = m["content"]
- if isinstance(prev_content, str) and isinstance(curr_content, str):
- fixed[-1]["content"] = prev_content + "\n" + curr_content
- elif isinstance(prev_content, list) and isinstance(curr_content, list):
- fixed[-1]["content"] = prev_content + curr_content
- else:
- if isinstance(prev_content, str):
- prev_content = [{"type": "text", "text": prev_content}]
- if isinstance(curr_content, str):
- curr_content = [{"type": "text", "text": curr_content}]
- fixed[-1]["content"] = prev_content + curr_content
- else:
- # Consecutive assistant messages — merge text content.
- # Propagate the orphan-strip signature-invalidation flag onto the
- # surviving (prev) dict so _manage_thinking_signatures still sees it.
- if m.get("_thinking_signature_invalidated"):
- fixed[-1]["_thinking_signature_invalidated"] = True
- # Drop thinking blocks from the *second* message: their
- # signature was computed against a different turn boundary
- # and becomes invalid once merged.
- if isinstance(m["content"], list):
- m["content"] = [
- b for b in m["content"]
- if not (isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"})
- ]
- prev_blocks = fixed[-1]["content"]
- curr_blocks = m["content"]
- if isinstance(prev_blocks, list) and isinstance(curr_blocks, list):
- fixed[-1]["content"] = prev_blocks + curr_blocks
- elif isinstance(prev_blocks, str) and isinstance(curr_blocks, str):
- fixed[-1]["content"] = prev_blocks + "\n" + curr_blocks
- else:
- if isinstance(prev_blocks, str):
- prev_blocks = [{"type": "text", "text": prev_blocks}]
- if isinstance(curr_blocks, str):
- curr_blocks = [{"type": "text", "text": curr_blocks}]
- fixed[-1]["content"] = prev_blocks + curr_blocks
- else:
- fixed.append(m)
- return fixed
-
-
-def _manage_thinking_signatures(
- result: List[Dict[str, Any]], base_url: str | None, model: str | None
-) -> None:
- """Strip or preserve thinking blocks based on endpoint type.
-
- Anthropic signs thinking blocks against the full turn content.
- Any upstream mutation (context compression, session truncation, orphan
- stripping, message merging) invalidates the signature, causing HTTP 400
- "Invalid signature in thinking block".
-
- Signatures are Anthropic-proprietary. Third-party endpoints (MiniMax,
- Azure AI Foundry, AWS Bedrock, self-hosted proxies) cannot validate them
- and will reject them outright. Kimi's /coding and DeepSeek's /anthropic
- endpoints speak the Anthropic protocol upstream but require unsigned
- thinking blocks (synthesised from ``reasoning_content``) to round-trip on
- replayed assistant tool-call messages. See hermes-agent#13848 (Kimi) and
- hermes-agent#16748 (DeepSeek).
-
- Nous Portal's ``/v1/messages`` route is the exception among third-party
- hosts: it proxies Claude to Anthropic/Vertex/Bedrock and validates the
- same signed thinking blocks. Sticky ``session_id`` keeps a conversation
- on one upstream instance so those signatures stay warm — stripping them
- here would 400 the first tool-loop turn ("thinking must be passed back").
- Portal therefore takes the native Anthropic replay path below.
-
- Mutates ``result`` in place.
- """
- _THINKING_TYPES = frozenset(("thinking", "redacted_thinking"))
- # Portal speaks Anthropic's thinking contract end-to-end; do not treat it
- # as a signature-blind proxy even though the host is not anthropic.com.
- _is_third_party = (
- _is_third_party_anthropic_endpoint(base_url)
- and not _is_nous_portal_endpoint(base_url)
- )
-
- last_assistant_idx = None
- for i in range(len(result) - 1, -1, -1):
- if result[i].get("role") == "assistant":
- last_assistant_idx = i
- break
-
- for idx, m in enumerate(result):
- if m.get("role") != "assistant" or not isinstance(m.get("content"), list):
- continue
-
- if _is_kimi_family_endpoint(base_url, model):
- # Kimi does not enforce thinking signatures — replay as-is
- # (shared cleanup below still strips cache markers + the internal flag).
- pass
- elif _is_deepseek_anthropic_endpoint(base_url):
- # DeepSeek: strip signed, preserve unsigned.
- new_content = []
- for b in m["content"]:
- if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES:
- new_content.append(b)
- continue
- if b.get("signature") or b.get("data"):
- # Signed (or redacted-with-data) — upstream can't validate, strip.
- continue
- new_content.append(b)
- m["content"] = new_content or [{"type": "text", "text": "(empty)"}]
- elif _is_third_party or idx != last_assistant_idx:
- # Third-party: strip ALL thinking blocks (signatures are proprietary).
- # Direct Anthropic: strip from non-latest assistant messages only.
- stripped = [
- b for b in m["content"]
- if not (isinstance(b, dict) and b.get("type") in _THINKING_TYPES)
- ]
- m["content"] = stripped or [{"type": "text", "text": "(thinking elided)"}]
- else:
- # Latest assistant on direct Anthropic: keep signed, downgrade unsigned
- # to text so the reasoning isn't lost.
- #
- # Exception: if orphan-stripping (or another structural mutation) removed
- # a tool_use block from THIS turn, every thinking signature on it was
- # computed against the original turn content and is now dead. Anthropic
- # rejects the turn either way — replaying the signed block 400s with
- # "thinking blocks in the latest assistant message cannot be modified",
- # and a bare signed block with no following tool_use is also invalid.
- # Demote ALL thinking blocks on this turn to text so the turn replays
- # cleanly and the model can re-plan from the surviving tool results.
- signature_dead = bool(m.get("_thinking_signature_invalidated"))
- new_content = []
- for b in m["content"]:
- if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES:
- new_content.append(b)
- continue
- if signature_dead:
- thinking_text = b.get("thinking", "")
- if thinking_text:
- new_content.append({"type": "text", "text": thinking_text})
- continue
- if b.get("type") == "redacted_thinking":
- # Redacted blocks use 'data' for the signature payload —
- # drop the block when 'data' is missing (can't be validated).
- if b.get("data"):
- new_content.append(b)
- elif b.get("signature"):
- new_content.append(b)
- else:
- thinking_text = b.get("thinking", "")
- if thinking_text:
- new_content.append({"type": "text", "text": thinking_text})
- m["content"] = new_content or [{"type": "text", "text": "(empty)"}]
-
- # Strip cache_control from any remaining thinking/redacted_thinking
- # blocks — cache markers interfere with signature validation.
- for b in m["content"]:
- if isinstance(b, dict) and b.get("type") in _THINKING_TYPES:
- b.pop("cache_control", None)
-
- # Drop the internal bookkeeping flag — it must never reach the API payload.
- m.pop("_thinking_signature_invalidated", None)
-
-
-def _evict_old_screenshots(result: List[Dict[str, Any]]) -> None:
- """Keep only the most recent ``_MAX_KEEP_IMAGES`` computer-use screenshots.
-
- Base64 images cost ~1,465 tokens each and accumulate across tool calls.
- Walk backward, keep the most recent N, replace older ones with a placeholder.
-
- Mutates ``result`` in place.
- """
- _MAX_KEEP_IMAGES = 3
- _image_count = 0
- for msg in reversed(result):
- content = msg.get("content")
- if not isinstance(content, list):
- continue
- for block in content:
- if not isinstance(block, dict) or block.get("type") != "tool_result":
- continue
- inner = block.get("content")
- if not isinstance(inner, list):
- continue
- has_image = any(
- isinstance(b, dict) and b.get("type") == "image"
- for b in inner
- )
- if not has_image:
- continue
- _image_count += 1
- if _image_count > _MAX_KEEP_IMAGES:
- block["content"] = [
- b if b.get("type") != "image"
- else {"type": "text", "text": "[screenshot removed to save context]"}
- for b in inner
- ]
-
-
-def _ensure_leading_user_turn(result: List[Dict[str, Any]]) -> None:
- """Anthropic requires messages[0] to have role=user.
-
- After a second context compaction on the auto path the summary can be
- emitted as role=assistant with nothing in front of it (the system prompt
- lives outside messages[] or is extracted into the separate ``system``
- param), so messages[0] ends up assistant and the Messages API rejects
- the request with HTTP 400 — often masked by a misleading
- "tool_use ids were found without tool_result blocks" error (#52160).
-
- Mirror the Bedrock Converse adapter, which unconditionally prepends a
- minimal user turn when the first message is not user
- (convert_messages_to_converse).
-
- The inserted text block must be non-whitespace: Anthropic separately
- rejects any text content block whose text is empty or whitespace-only
- ("text content blocks must contain non-whitespace text"), so a single
- space here traded the "leading assistant turn" 400 for that one (#69512
- class). Uses the same placeholder as every other synthesized filler
- block in this module for consistency.
- """
- if result and result[0].get("role") != "user":
- result.insert(
- 0, {"role": "user", "content": [{"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER}]}
- )
-
-
-def _fix_blank_text_blocks_in_list(
- blocks: List[Any],
- *,
- placeholder_text: str,
- msg_index: int,
- role: Any,
- location: str,
-) -> List[Any]:
- """Drop blank/whitespace-only text blocks from ``blocks``, in place logic.
-
- Non-text blocks (tool_use, tool_result, image, document, thinking, …)
- and the relative order of everything else are left untouched. A
- cache_control marker riding on a dropped block is relocated onto the
- last surviving text/tool_use block so a breakpoint is never silently
- lost. If nothing survives, a single non-blank placeholder text block
- takes the dropped blocks' place (carrying the relocated cache_control,
- if any) so the message never has empty content.
-
- Returns a new list; does not mutate ``blocks``.
- """
- kept: List[Any] = []
- relocated_cache_control = None
- for block_index, blk in enumerate(blocks):
- if (
- isinstance(blk, dict)
- and blk.get("type") == "text"
- and not (isinstance(blk.get("text"), str) and blk["text"].strip())
- ):
- if isinstance(blk.get("cache_control"), dict):
- relocated_cache_control = blk["cache_control"]
- logger.warning(
- "Pre-call sanitizer: dropped blank text content block "
- "(message_index=%d role=%s location=%s block_index=%d "
- "block_type=text)",
- msg_index,
- role,
- location,
- block_index,
- )
- continue
- kept.append(blk)
- if not kept:
- placeholder: Dict[str, Any] = {"type": "text", "text": placeholder_text}
- if relocated_cache_control is not None:
- placeholder["cache_control"] = relocated_cache_control
- kept.append(placeholder)
- elif relocated_cache_control is not None:
- _apply_assistant_cache_control_to_last_cacheable_block(kept, relocated_cache_control)
- return kept
-
-
-def _scrub_blank_text_blocks(result: List[Dict[str, Any]]) -> None:
- """Final provider-boundary guard against blank Anthropic text blocks.
-
- Anthropic rejects any text content block whose ``text`` is empty or
- whitespace-only with HTTP 400 ("text content blocks must contain
- non-whitespace text"). ``_convert_assistant_message``,
- ``_convert_user_message`` and ``_ensure_leading_user_turn`` already
- avoid emitting these for the paths that build them, but this pass runs
- last — after every other transform in ``convert_messages_to_anthropic``
- — so a blank block from any current or future producer (including one
- nested inside a ``tool_result``'s own content list) never reaches the
- wire. Diagnostics are structural only: message index, role, content
- location, block index/type. Never logs message text, tool arguments,
- tokens, or credentials. Mutates ``result`` in place.
- """
- for msg_index, msg in enumerate(result):
- if not isinstance(msg, dict):
- continue
- role = msg.get("role")
- content = msg.get("content")
- if not isinstance(content, list) or not content:
- continue
- placeholder_text = _EMPTY_TEXT_PLACEHOLDER if role == "assistant" else "(empty message)"
- new_content = _fix_blank_text_blocks_in_list(
- content,
- placeholder_text=placeholder_text,
- msg_index=msg_index,
- role=role,
- location="content",
- )
- for blk in new_content:
- if not isinstance(blk, dict) or blk.get("type") != "tool_result":
- continue
- inner = blk.get("content")
- if isinstance(inner, list) and inner:
- blk["content"] = _fix_blank_text_blocks_in_list(
- inner,
- placeholder_text="(no output)",
- msg_index=msg_index,
- role=role,
- location="tool_result",
- )
- msg["content"] = new_content
-
-
-def convert_messages_to_anthropic(
- messages: List[Dict],
- base_url: str | None = None,
- model: str | None = None,
-) -> Tuple[Optional[Any], List[Dict]]:
- """Convert OpenAI-format messages to Anthropic format.
-
- Returns (system_prompt, anthropic_messages).
- System messages are extracted since Anthropic takes them as a separate param.
- system_prompt is a string or list of content blocks (when cache_control present).
-
- When *base_url* is provided and points to a third-party Anthropic-compatible
- endpoint, all thinking block signatures are stripped. Signatures are
- Anthropic-proprietary — third-party endpoints cannot validate them and will
- reject them with HTTP 400 "Invalid signature in thinking block".
-
- When *model* is provided and matches the Kimi / Moonshot family (or
- *base_url* is a Kimi / Moonshot host), unsigned thinking blocks
- synthesised from ``reasoning_content`` are preserved on replayed
- assistant tool-call messages — Kimi requires the field to exist, even
- if empty.
- """
- system = None
- result: List[Dict[str, Any]] = []
-
- for m in messages:
- role = m.get("role", "user")
- content = m.get("content", "")
-
- if role == "system":
- if isinstance(content, list):
- # Preserve cache_control markers on content blocks
- has_cache = any(
- p.get("cache_control") for p in content if isinstance(p, dict)
- )
- if has_cache:
- # Copy blocks before coercing so the caller's message
- # dicts are never mutated, then replace blank/whitespace
- # text with the shared non-whitespace placeholder —
- # Anthropic rejects a blank system text block with the
- # same HTTP 400 as message blocks ("text content blocks
- # must contain non-whitespace text"), and a blank block
- # carrying a cache_control breakpoint cannot simply be
- # dropped (#70909).
- system = []
- for p in content:
- if not isinstance(p, dict):
- continue
- if (
- p.get("type") == "text"
- and isinstance(p.get("text"), str)
- and not p["text"].strip()
- ):
- p = dict(p)
- p["text"] = _EMPTY_TEXT_PLACEHOLDER
- system.append(p)
- else:
- system = "\n".join(
- p["text"] for p in content if p.get("type") == "text"
- )
- else:
- system = content
- continue
-
- if role == "assistant":
- result.append(_convert_assistant_message(m))
- continue
-
- if role == "tool":
- _convert_tool_message_to_result(result, m)
- continue
-
- # Regular user message
- result.append(_convert_user_message(content))
-
- _strip_orphaned_tool_blocks(result)
- result = _merge_consecutive_roles(result)
- _ensure_leading_user_turn(result)
- _manage_thinking_signatures(result, base_url, model)
- _evict_old_screenshots(result)
- _scrub_blank_text_blocks(result)
-
- return system, result
def build_anthropic_kwargs(
diff --git a/agent/anthropic_credentials.py b/agent/anthropic_credentials.py
new file mode 100644
index 00000000000..a48d6f0edef
--- /dev/null
+++ b/agent/anthropic_credentials.py
@@ -0,0 +1,910 @@
+"""Anthropic credential sources, OAuth flows, and token resolution.
+
+Extracted from ``agent/anthropic_adapter.py``: the adapter is a message/HTTP
+translation layer, while everything below owns *where an Anthropic credential
+comes from* and *how a rotated one is committed*. Keeping the two apart means
+the refresh transaction has a single home instead of being interleaved with
+request building.
+
+Sources, in the order ``resolve_anthropic_token()`` consults them:
+
+1. ``ANTHROPIC_TOKEN`` / ``CLAUDE_CODE_OAUTH_TOKEN`` (explicit OAuth env)
+2. ``ANTHROPIC_API_KEY`` (explicit API key)
+3. ``~/.hermes/.anthropic_oauth.json`` (Hermes PKCE login)
+4. ``~/.claude/.credentials.json`` / macOS Keychain (Claude Code)
+5. the credential pool in ``auth.json``
+
+Sources 3 and 4 are *singletons*: ``credential_pool._seed_from_singletons()``
+re-reads them on every ``load_pool()`` and writes what it finds over the pool
+row, which is why a failed write here is a failed refresh (see
+``CredentialPersistError``) rather than a best-effort cache miss.
+
+``agent.anthropic_adapter`` re-exports every public name below, so existing
+``from agent.anthropic_adapter import resolve_anthropic_token`` imports keep
+working.
+"""
+
+import json
+import logging
+import os
+import platform
+import secrets
+import stat
+import subprocess
+from pathlib import Path
+from typing import Any, Dict, Optional
+
+from hermes_constants import get_hermes_home
+from agent.secret_scope import get_secret as _get_secret
+
+logger = logging.getLogger(__name__)
+
+
+def _getenv(name: str, default: str = "") -> str:
+ """Profile-scoped replacement for os.getenv on credential reads.
+
+ Routes through the secret scope (Workstream A): identical to os.getenv
+ when multiplexing is off, scope-aware (and fail-closed on an unscoped
+ read) when on. Mirrors the same wrapper in hermes_cli/runtime_provider.py.
+ """
+ val = _get_secret(name, default)
+ return val if val is not None else default
+
+
+def _is_oauth_token(key: str) -> bool:
+ """Check if the key is an Anthropic OAuth/setup token.
+
+ Positively identifies Anthropic OAuth tokens by their key format:
+ - ``sk-ant-`` prefix (but NOT ``sk-ant-api``) → setup tokens, managed keys
+ - ``eyJ`` prefix → JWTs from the Anthropic OAuth flow
+ - ``cc-`` prefix → Claude Code OAuth access tokens (from CLAUDE_CODE_OAUTH_TOKEN)
+
+ Non-Anthropic keys (MiniMax, Alibaba, etc.) don't match any pattern
+ and correctly return False.
+ """
+ if not key:
+ return False
+ # Regular Anthropic Console API keys — x-api-key auth, never OAuth
+ if key.startswith("sk-ant-api"):
+ return False
+ # Anthropic-issued tokens (setup-tokens sk-ant-oat-*, managed keys)
+ if key.startswith("sk-ant-"):
+ return True
+ # JWTs from Anthropic OAuth flow
+ if key.startswith("eyJ"):
+ return True
+ # Claude Code OAuth access tokens (opaque, from CLAUDE_CODE_OAUTH_TOKEN)
+ if key.startswith("cc-"):
+ return True
+ return False
+
+
+
+class CredentialPersistError(RuntimeError):
+ """A rotated single-use credential could not be durably committed.
+
+ Anthropic OAuth refresh tokens are single-use: a successful refresh POST
+ consumes the old refresh token server-side and returns a replacement. The
+ replacement exists only in memory until it reaches its authoritative
+ on-disk store (``~/.claude/.credentials.json`` for ``claude_code``,
+ ``~/.hermes/.anthropic_oauth.json`` for ``hermes_pkce``).
+
+ If that write fails and the caller reports success anyway, the on-disk
+ (already consumed) pair survives and is re-seeded on the next
+ ``load_pool()``, so the following refresh replays a spent token and fails
+ with ``invalid_grant`` / ``refresh_token_reused``. Callers must therefore
+ treat this as a failed refresh, not a successful one, and fail closed.
+ """
+
+ def __init__(self, path: Any, cause: BaseException) -> None:
+ super().__init__(
+ f"failed to durably persist rotated Anthropic credentials to {path}: {cause}"
+ )
+ self.path = path
+
+
+def _read_claude_code_credentials_from_keychain() -> Optional[Dict[str, Any]]:
+ """Read Claude Code OAuth credentials from the macOS Keychain.
+
+ Claude Code >=2.1.114 stores credentials in the macOS Keychain under the
+ service name "Claude Code-credentials" rather than (or in addition to)
+ the JSON file at ~/.claude/.credentials.json.
+
+ The password field contains a JSON string with the same claudeAiOauth
+ structure as the JSON file.
+
+ Returns dict with {accessToken, refreshToken?, expiresAt?} or None.
+ """
+ if platform.system() != "Darwin":
+ return None
+
+ try:
+ # Read the "Claude Code-credentials" generic password entry
+ result = subprocess.run(
+ ["security", "find-generic-password",
+ "-s", "Claude Code-credentials",
+ "-w"],
+ capture_output=True,
+ text=True, encoding='utf-8', errors='replace',
+ timeout=5,
+ stdin=subprocess.DEVNULL,
+ )
+ except (OSError, subprocess.TimeoutExpired):
+ logger.debug("Keychain: security command not available or timed out")
+ return None
+
+ if result.returncode != 0:
+ logger.debug("Keychain: no entry found for 'Claude Code-credentials'")
+ return None
+
+ raw = result.stdout.strip()
+ if not raw:
+ return None
+
+ try:
+ data = json.loads(raw)
+ except json.JSONDecodeError:
+ logger.debug("Keychain: credentials payload is not valid JSON")
+ return None
+
+ oauth_data = data.get("claudeAiOauth")
+ if oauth_data and isinstance(oauth_data, dict):
+ access_token = oauth_data.get("accessToken", "")
+ if access_token:
+ return {
+ "accessToken": access_token,
+ "refreshToken": oauth_data.get("refreshToken", ""),
+ "expiresAt": oauth_data.get("expiresAt", 0),
+ "source": "macos_keychain",
+ }
+
+ return None
+
+
+def claude_code_credentials_path() -> Path:
+ """Location Claude Code CLI writes its shared OAuth credentials file.
+
+ This file is not profile-owned: every Hermes profile's credential pool
+ reads and writes the *same* path, so cross-profile refresh races on a
+ ``claude_code`` pool entry must be serialized against this exact path
+ (see ``CredentialPool._claude_code_credentials_lock`` in
+ ``agent/credential_pool.py``).
+ """
+ return Path.home() / ".claude" / ".credentials.json"
+
+
+def _read_claude_code_credentials_from_file() -> Optional[Dict[str, Any]]:
+ """Read Claude Code OAuth credentials from ~/.claude/.credentials.json.
+
+ Returns dict with {accessToken, refreshToken?, expiresAt?, source} or None.
+ """
+ cred_path = claude_code_credentials_path()
+ if not cred_path.exists():
+ return None
+ try:
+ data = json.loads(cred_path.read_text(encoding="utf-8"))
+ except (json.JSONDecodeError, OSError, IOError) as e:
+ logger.debug("Failed to read ~/.claude/.credentials.json: %s", e)
+ return None
+
+ oauth_data = data.get("claudeAiOauth")
+ if not (oauth_data and isinstance(oauth_data, dict)):
+ return None
+ access_token = oauth_data.get("accessToken", "")
+ if not access_token:
+ return None
+ return {
+ "accessToken": access_token,
+ "refreshToken": oauth_data.get("refreshToken", ""),
+ "expiresAt": oauth_data.get("expiresAt", 0),
+ "source": "claude_code_credentials_file",
+ }
+
+
+def read_claude_code_credentials() -> Optional[Dict[str, Any]]:
+ """Read refreshable Claude Code OAuth credentials.
+
+ Reads from two possible sources and reconciles them:
+ 1. macOS Keychain (Darwin only) — "Claude Code-credentials" entry
+ 2. ~/.claude/.credentials.json file
+
+ Selection rules when both are present:
+ - If exactly one is non-expired, prefer that one. (Handles the case
+ where Claude Code refreshes one source but not the other — observed
+ in the wild on Claude Code 2.1.x.)
+ - Otherwise, prefer the source with the later ``expiresAt`` so that
+ any subsequent refresh uses the most recent ``refreshToken``.
+
+ This intentionally excludes ~/.claude.json primaryApiKey. Opencode's
+ subscription flow is OAuth/setup-token based with refreshable credentials,
+ and native direct Anthropic provider usage should follow that path rather
+ than auto-detecting Claude's first-party managed key.
+
+ Returns dict with {accessToken, refreshToken?, expiresAt?, source} or None.
+ """
+ kc_creds = _read_claude_code_credentials_from_keychain()
+ file_creds = _read_claude_code_credentials_from_file()
+
+ if kc_creds and file_creds:
+ kc_valid = is_claude_code_token_valid(kc_creds)
+ file_valid = is_claude_code_token_valid(file_creds)
+ if kc_valid and not file_valid:
+ return kc_creds
+ if file_valid and not kc_valid:
+ return file_creds
+ # Both valid or both expired: prefer the later expiresAt so the
+ # downstream refresh path uses the freshest refresh_token.
+ kc_exp = kc_creds.get("expiresAt", 0) or 0
+ file_exp = file_creds.get("expiresAt", 0) or 0
+ return kc_creds if kc_exp >= file_exp else file_creds
+
+ return kc_creds or file_creds
+
+
+def is_claude_code_token_valid(creds: Dict[str, Any]) -> bool:
+ """Check if Claude Code credentials have a non-expired access token."""
+ import time
+
+ expires_at = creds.get("expiresAt", 0)
+ if not expires_at:
+ # No expiry set (managed keys) — valid if token is present
+ return bool(creds.get("accessToken"))
+
+ # expiresAt is in milliseconds since epoch
+ now_ms = int(time.time() * 1000)
+ # Allow 60 seconds of buffer
+ return now_ms < (expires_at - 60_000)
+
+
+def refresh_anthropic_oauth_pure(refresh_token: str, *, use_json: bool = False) -> Dict[str, Any]:
+ """Refresh an Anthropic OAuth token without mutating local credential files."""
+ import time
+ import urllib.parse
+ import urllib.request
+
+ if not refresh_token:
+ raise ValueError("refresh_token is required")
+
+ client_id = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
+ if use_json:
+ data = json.dumps({
+ "grant_type": "refresh_token",
+ "refresh_token": refresh_token,
+ "client_id": client_id,
+ }).encode()
+ content_type = "application/json"
+ else:
+ data = urllib.parse.urlencode({
+ "grant_type": "refresh_token",
+ "refresh_token": refresh_token,
+ "client_id": client_id,
+ }).encode()
+ content_type = "application/x-www-form-urlencoded"
+
+ token_endpoints = [
+ "https://platform.claude.com/v1/oauth/token",
+ "https://console.anthropic.com/v1/oauth/token",
+ ]
+ last_error = None
+ for endpoint in token_endpoints:
+ req = urllib.request.Request(
+ endpoint,
+ data=data,
+ headers={
+ "Content-Type": content_type,
+ "User-Agent": _OAUTH_TOKEN_USER_AGENT,
+ },
+ method="POST",
+ )
+ try:
+ with urllib.request.urlopen(req, timeout=10) as resp:
+ result = json.loads(resp.read().decode())
+ except Exception as exc:
+ last_error = exc
+ logger.debug("Anthropic token refresh failed at %s: %s", endpoint, exc)
+ continue
+
+ access_token = result.get("access_token", "")
+ if not access_token:
+ raise ValueError("Anthropic refresh response was missing access_token")
+ next_refresh = result.get("refresh_token", refresh_token)
+ expires_in = result.get("expires_in", 3600)
+ return {
+ "access_token": access_token,
+ "refresh_token": next_refresh,
+ "expires_at_ms": int(time.time() * 1000) + (expires_in * 1000),
+ }
+
+ if last_error is not None:
+ raise last_error
+ raise ValueError("Anthropic token refresh failed")
+
+
+def _refresh_oauth_token(creds: Dict[str, Any]) -> Optional[str]:
+ """Attempt to refresh an expired Claude Code OAuth token.
+
+ Claude Code's OAuth refresh tokens are single-use: a successful refresh
+ rotates the pair and invalidates the old refresh token. Claude Code itself
+ also refreshes on its own schedule (IDE/CLI activity), so by the time
+ Hermes notices an expired token, Claude Code may have already rotated it.
+ POSTing our now-stale refresh token in that window races Claude Code and
+ fails with ``invalid_grant``.
+
+ So before refreshing, re-read the live credential sources. If Claude Code
+ has already produced a valid token, adopt it and skip the POST entirely.
+ Only fall back to refreshing ourselves when no fresh credential is found.
+ """
+ # Claude Code may have already refreshed — adopt its token rather than
+ # racing it with our (possibly already-rotated) refresh token. The read,
+ # decision, POST, and write-back all belong to the shared credentials
+ # source, so hold the same path-keyed cross-process lock used by the pool.
+ # Without this direct resolver path, two profiles can still spend one
+ # single-use refresh token even though CredentialPool is serialized.
+ try:
+ from hermes_cli.auth import AUTH_LOCK_TIMEOUT_SECONDS, _auth_store_lock, env_float
+
+ refresh_timeout_seconds = env_float(
+ "HERMES_ANTHROPIC_REFRESH_TIMEOUT_SECONDS", 20
+ )
+ lock_timeout_seconds = max(
+ float(AUTH_LOCK_TIMEOUT_SECONDS),
+ float(refresh_timeout_seconds) + 5.0,
+ )
+ with _auth_store_lock(
+ timeout_seconds=lock_timeout_seconds,
+ target_path=claude_code_credentials_path(),
+ ):
+ # Only adopt when the live re-read produced a DIFFERENT token with
+ # a real future expiry: re-adopting the same credential we were
+ # just handed would be a no-op, and a 0/absent ``expiresAt`` means
+ # "managed key / unknown expiry" (see is_claude_code_token_valid).
+ current = read_claude_code_credentials()
+ if current:
+ current_token = current.get("accessToken", "")
+ current_exp = current.get("expiresAt", 0) or 0
+ if (
+ current_token
+ and current_token != creds.get("accessToken", "")
+ and current_exp > 0
+ and is_claude_code_token_valid(current)
+ ):
+ logger.debug("Adopted Claude Code's already-refreshed OAuth token")
+ return current_token
+
+ refresh_token = (
+ (current or {}).get("refreshToken", "")
+ or creds.get("refreshToken", "")
+ )
+ if not refresh_token:
+ logger.debug("No refresh token available — cannot refresh")
+ return None
+
+ try:
+ refreshed = refresh_anthropic_oauth_pure(refresh_token, use_json=False)
+ except Exception as e:
+ logger.debug("Failed to refresh Claude Code token: %s", e)
+ return None
+
+ # The POST above already consumed ``refresh_token`` server-side.
+ # Writing the replacement pair is the commit step of that
+ # transaction, not a cache update: if it fails, the rotation is
+ # unrecoverable and the pair still on disk is spent. Fail closed
+ # rather than handing back an access token whose refresh half was
+ # lost — reporting success here is what lets a later load replay
+ # the consumed token and produce ``invalid_grant``.
+ try:
+ _write_claude_code_credentials(
+ refreshed["access_token"],
+ refreshed["refresh_token"],
+ refreshed["expires_at_ms"],
+ )
+ except Exception as e:
+ logger.error(
+ "Anthropic OAuth refresh rotated the single-use token but could not "
+ "commit it to %s (%s) — treating the refresh as failed; "
+ "re-run 'claude setup-token' to reauthenticate",
+ claude_code_credentials_path(),
+ e,
+ )
+ return None
+
+ logger.debug("Successfully refreshed Claude Code OAuth token")
+ return refreshed["access_token"]
+ except Exception as e:
+ # Lock acquisition/read failures should preserve the resolver's
+ # existing fail-soft contract rather than taking down agent startup.
+ logger.debug("Failed to acquire Claude Code refresh lock: %s", e)
+ return None
+
+
+def _write_claude_code_credentials(
+ access_token: str,
+ refresh_token: str,
+ expires_at_ms: int,
+ *,
+ scopes: Optional[list] = None,
+) -> None:
+ """Write refreshed credentials back to ~/.claude/.credentials.json.
+
+ The optional *scopes* list (e.g. ``["user:inference", "user:profile", ...]``)
+ is persisted so that Claude Code's own auth check recognises the credential
+ as valid. Claude Code >=2.1.81 gates on the presence of ``"user:inference"``
+ in the stored scopes before it will use the token.
+
+ Raises ``CredentialPersistError`` when the rotated pair does not reach the
+ file. This write is the commit step of the refresh transaction, not a
+ best-effort cache update: a swallowed failure leaves the consumed
+ pre-rotation pair on disk to be re-seeded and replayed (see
+ ``CredentialPersistError``).
+ """
+ cred_path = claude_code_credentials_path()
+ try:
+ # Read existing file to preserve other fields
+ existing = {}
+ if cred_path.exists():
+ existing = json.loads(cred_path.read_text(encoding="utf-8"))
+
+ oauth_data: Dict[str, Any] = {
+ "accessToken": access_token,
+ "refreshToken": refresh_token,
+ "expiresAt": expires_at_ms,
+ }
+ if scopes is not None:
+ oauth_data["scopes"] = scopes
+ elif "claudeAiOauth" in existing and "scopes" in existing["claudeAiOauth"]:
+ # Preserve previously-stored scopes when the refresh response
+ # does not include a scope field.
+ oauth_data["scopes"] = existing["claudeAiOauth"]["scopes"]
+
+ existing["claudeAiOauth"] = oauth_data
+
+ cred_path.parent.mkdir(parents=True, exist_ok=True)
+ # Per-process random suffix avoids collisions between concurrent
+ # writers and stale leftovers from a prior crashed write.
+ _tmp_cred = cred_path.with_suffix(f".tmp.{os.getpid()}.{secrets.token_hex(4)}")
+ try:
+ # Create the temp file atomically at 0o600. The previous
+ # write_text + post-replace chmod opened a TOCTOU window where
+ # both the temp file and the destination briefly inherited the
+ # process umask (commonly 0o644 = world-readable), exposing
+ # Claude Code OAuth tokens to other local users between create
+ # and chmod. Mirrors agent/google_oauth.py (#19673) and
+ # tools/mcp_oauth.py (#21148). Parent dir (~/.claude/) is
+ # owned by Claude Code itself, so we leave its mode alone.
+ fd = os.open(
+ str(_tmp_cred),
+ os.O_WRONLY | os.O_CREAT | os.O_EXCL,
+ stat.S_IRUSR | stat.S_IWUSR,
+ )
+ with os.fdopen(fd, "w", encoding="utf-8") as fh:
+ json.dump(existing, fh, indent=2)
+ fh.flush()
+ os.fsync(fh.fileno())
+ os.replace(_tmp_cred, cred_path)
+ except OSError:
+ try:
+ _tmp_cred.unlink(missing_ok=True)
+ except OSError:
+ pass
+ raise
+ except (OSError, IOError, ValueError) as e:
+ # ValueError covers a corrupt existing file (JSONDecodeError): the
+ # merge-read is part of the commit, so failing it means the rotated
+ # pair never landed either.
+ logger.error("Failed to write refreshed credentials to %s: %s", cred_path, e)
+ raise CredentialPersistError(cred_path, e) from e
+
+
+def _resolve_claude_code_token_from_credentials(creds: Optional[Dict[str, Any]] = None) -> Optional[str]:
+ """Resolve a token from Claude Code credential files, refreshing if needed."""
+ creds = creds or read_claude_code_credentials()
+ if creds and is_claude_code_token_valid(creds):
+ logger.debug("Using Claude Code credentials (auto-detected)")
+ return creds["accessToken"]
+ if creds:
+ logger.debug("Claude Code credentials expired — attempting refresh")
+ refreshed = _refresh_oauth_token(creds)
+ if refreshed:
+ return refreshed
+ logger.debug("Token refresh failed — re-run 'claude setup-token' to reauthenticate")
+ return None
+
+
+def _prefer_refreshable_claude_code_token(env_token: str, creds: Optional[Dict[str, Any]]) -> Optional[str]:
+ """Prefer Claude Code creds when a persisted env OAuth token would shadow refresh.
+
+ Hermes historically persisted setup tokens into ANTHROPIC_TOKEN. That makes
+ later refresh impossible because the static env token wins before we ever
+ inspect Claude Code's refreshable credential file. If we have a refreshable
+ Claude Code credential record, prefer it over the static env OAuth token.
+ """
+ if not env_token or not _is_oauth_token(env_token) or not isinstance(creds, dict):
+ return None
+ if not creds.get("refreshToken"):
+ return None
+
+ resolved = _resolve_claude_code_token_from_credentials(creds)
+ if resolved and resolved != env_token:
+ logger.debug(
+ "Preferring Claude Code credential file over static env OAuth token so refresh can proceed"
+ )
+ return resolved
+ return None
+
+
+def _resolve_anthropic_pool_token() -> Optional[str]:
+ """Return the first available Anthropic OAuth token from credential_pool.
+
+ Read-only: enumerates with ``clear_expired=False, refresh=False`` so a bare
+ token *resolve* (which runs from diagnostic/read-only call sites such as
+ ``account_usage`` and ``hermes models``) never mutates ``~/.hermes/auth.json``
+ or makes a network refresh call. Refresh-on-expiry is owned by the API call
+ path's pool recovery, not the resolver.
+ """
+ try:
+ from agent.credential_pool import AUTH_TYPE_OAUTH, load_pool
+ except Exception:
+ return None
+
+ try:
+ pool = load_pool("anthropic")
+ # Enumerate read-only (clear_expired=False, refresh=False): never persist
+ # to auth.json or trigger a network refresh from a bare resolve. select()
+ # is deliberately NOT used — it runs clear_expired=True, refresh=True,
+ # which would violate this read-only contract.
+ entries, _pending = pool._available_entries(clear_expired=False, refresh=False)
+ except Exception:
+ logger.debug("Failed to read Anthropic credential_pool", exc_info=True)
+ return None
+
+ for entry in entries:
+ if getattr(entry, "auth_type", None) != AUTH_TYPE_OAUTH:
+ continue
+ # access_token is a declared field but a persisted entry can carry an
+ # explicit null (or a partially-written OAuth entry), so coerce before
+ # strip — a bare None.strip() here would escape the try/excepts above
+ # and crash the whole resolver, taking down the source #5 fallback too.
+ # Matches the aux-client analog (auxiliary_client.py: str(key or "")).
+ token = (getattr(entry, "access_token", None) or "").strip()
+ if token:
+ return token
+
+ return None
+
+
+def resolve_anthropic_token() -> Optional[str]:
+ """Resolve an Anthropic token from all available sources.
+
+ Priority:
+ 1. ANTHROPIC_TOKEN env var (OAuth/setup token saved by Hermes)
+ 2. CLAUDE_CODE_OAUTH_TOKEN env var
+ 3. ANTHROPIC_API_KEY env var (explicit regular API key)
+ 4. Claude Code credentials (~/.claude.json or ~/.claude/.credentials.json)
+ — with automatic refresh if expired and a refresh token is available
+ 5. Anthropic credential_pool OAuth entry (~/.hermes/auth.json)
+
+ Returns the token string or None.
+ """
+ creds: Optional[Dict[str, Any]] = None
+ creds_loaded = False
+
+ def _read_creds() -> Optional[Dict[str, Any]]:
+ nonlocal creds, creds_loaded
+ if not creds_loaded:
+ creds = read_claude_code_credentials()
+ creds_loaded = True
+ return creds
+
+ # 1. Hermes-managed OAuth/setup token env var
+ token = _getenv("ANTHROPIC_TOKEN").strip()
+ if token:
+ preferred = _prefer_refreshable_claude_code_token(token, _read_creds())
+ if preferred:
+ return preferred
+ return token
+
+ # 2. CLAUDE_CODE_OAUTH_TOKEN (used by Claude Code for setup-tokens)
+ cc_token = _getenv("CLAUDE_CODE_OAUTH_TOKEN").strip()
+ if cc_token:
+ preferred = _prefer_refreshable_claude_code_token(cc_token, _read_creds())
+ if preferred:
+ return preferred
+ return cc_token
+
+ # 3. Regular API key. An explicit user-configured key must not be shadowed
+ # by auto-discovered Claude Code or credential-pool OAuth credentials.
+ api_key = _getenv("ANTHROPIC_API_KEY").strip()
+ if api_key:
+ return api_key
+
+ # 4. Claude Code credential file
+ resolved_claude_token = _resolve_claude_code_token_from_credentials(_read_creds())
+ if resolved_claude_token:
+ return resolved_claude_token
+
+ # 5. Hermes credential_pool OAuth entry.
+ resolved_pool_token = _resolve_anthropic_pool_token()
+ if resolved_pool_token:
+ return resolved_pool_token
+
+ return None
+
+
+def run_oauth_setup_token() -> Optional[str]:
+ """Run 'claude setup-token' interactively and return the resulting token.
+
+ Checks multiple sources after the subprocess completes:
+ 1. Claude Code credential files (may be written by the subprocess)
+ 2. CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_TOKEN env vars
+
+ Returns the token string, or None if no credentials were obtained.
+ Raises FileNotFoundError if the 'claude' CLI is not installed.
+ """
+ import shutil
+ import subprocess
+
+ claude_path = shutil.which("claude")
+ if not claude_path:
+ raise FileNotFoundError(
+ "The 'claude' CLI is not installed. "
+ "Install it with: npm install -g @anthropic-ai/claude-code"
+ )
+
+ # Run interactively — stdin/stdout/stderr inherited so the user can
+ # complete the OAuth login prompt. Must keep inherited stdin; the TUI-EOF
+ # concern does not apply to an interactive login the user explicitly
+ # invokes. noqa: subprocess-stdin
+ try:
+ subprocess.run([claude_path, "setup-token"])
+ except (KeyboardInterrupt, EOFError):
+ return None
+
+ # Check if credentials were saved to Claude Code's config files
+ creds = read_claude_code_credentials()
+ if creds and is_claude_code_token_valid(creds):
+ return creds["accessToken"]
+
+ # Check env vars that may have been set
+ for env_var in ("CLAUDE_CODE_OAUTH_TOKEN", "ANTHROPIC_TOKEN"):
+ val = _getenv(env_var).strip()
+ if val:
+ return val
+
+ return None
+
+
+# ── Hermes-native PKCE OAuth flow ────────────────────────────────────────
+# Mirrors the flow used by Claude Code, pi-ai, and OpenCode.
+# Stores credentials in ~/.hermes/.anthropic_oauth.json (our own file).
+
+_OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
+# Anthropic migrated the OAuth token endpoint to platform.claude.com;
+# console.anthropic.com now 404s. Callers should iterate _OAUTH_TOKEN_URLS
+# (new host first, console fallback). _OAUTH_TOKEN_URL is kept as the primary
+# for backward compatibility with existing imports and now points at the live host.
+_OAUTH_TOKEN_URLS = [
+ "https://platform.claude.com/v1/oauth/token",
+ "https://console.anthropic.com/v1/oauth/token",
+]
+_OAUTH_TOKEN_URL = _OAUTH_TOKEN_URLS[0]
+# User-Agent sent on the OAuth *token endpoint* (login exchange + refresh).
+# Anthropic rate-limits (HTTP 429) any token-endpoint request whose UA starts
+# with ``claude-code/`` — verified empirically against platform.claude.com:
+# ``claude-code/2.1.200`` and ``Mozilla/5.0`` -> 429; ``axios/*``, ``node``,
+# and SDK-style UAs -> 400 (reached code validation). The real Claude Code CLI
+# exchanges the auth code with a bare axios client (``axios/<ver>``), NOT its
+# ``claude-code/`` inference UA. We mirror that here. NOTE: the *inference* path
+# (build_anthropic_kwargs) still uses the ``claude-code/`` UA + ``x-app: cli`` —
+# that fingerprint is required there and is NOT throttled on the messages API.
+_OAUTH_TOKEN_USER_AGENT = "axios/1.7.9"
+_OAUTH_REDIRECT_URI = "https://console.anthropic.com/oauth/code/callback"
+_OAUTH_SCOPES = "org:create_api_key user:profile user:inference"
+def _get_hermes_oauth_file() -> Path:
+ return get_hermes_home() / ".anthropic_oauth.json"
+
+
+def _generate_pkce() -> tuple:
+ """Generate PKCE code_verifier and code_challenge (S256)."""
+ import base64
+ import hashlib
+ import secrets
+
+ verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
+ challenge = base64.urlsafe_b64encode(
+ hashlib.sha256(verifier.encode()).digest()
+ ).rstrip(b"=").decode()
+ return verifier, challenge
+
+
+def run_hermes_oauth_login_pure() -> Optional[Dict[str, Any]]:
+ """Run Hermes-native OAuth PKCE flow and return credential state."""
+ import secrets
+ import time
+ import webbrowser
+
+ verifier, challenge = _generate_pkce()
+ oauth_state = secrets.token_urlsafe(32)
+
+ params = {
+ "code": "true",
+ "client_id": _OAUTH_CLIENT_ID,
+ "response_type": "code",
+ "redirect_uri": _OAUTH_REDIRECT_URI,
+ "scope": _OAUTH_SCOPES,
+ "code_challenge": challenge,
+ "code_challenge_method": "S256",
+ "state": oauth_state,
+ }
+ from urllib.parse import urlencode
+
+ auth_url = f"https://claude.ai/oauth/authorize?{urlencode(params)}"
+
+ print()
+ print("Authorize Hermes with your Claude Pro/Max subscription.")
+ print()
+ print("╭─ Claude Pro/Max Authorization ────────────────────╮")
+ print("│ │")
+ print("│ Open this link in your browser: │")
+ print("╰───────────────────────────────────────────────────╯")
+ print()
+ print(f" {auth_url}")
+ print()
+
+ try:
+ from hermes_cli.auth import _can_open_graphical_browser as _can_open_gui
+ except Exception:
+ _can_open_gui = lambda: True # noqa: E731 — degrade to prior behavior
+
+ if _can_open_gui():
+ try:
+ webbrowser.open(auth_url)
+ print(" (Browser opened automatically)")
+ except Exception:
+ pass
+
+ print()
+ print("After authorizing, you'll see a code. Paste it below.")
+ print()
+ try:
+ auth_code = input("Authorization code: ").strip()
+ except (KeyboardInterrupt, EOFError):
+ return None
+
+ if not auth_code:
+ print("No code entered.")
+ return None
+
+ splits = auth_code.split("#")
+ code = splits[0]
+ received_state = splits[1] if len(splits) > 1 else ""
+
+ # Validate state to prevent CSRF (RFC 6749 §10.12)
+ if received_state != oauth_state:
+ logger.warning("OAuth state mismatch — possible CSRF, aborting")
+ return None
+
+ try:
+ import urllib.request
+
+ exchange_data = json.dumps({
+ "grant_type": "authorization_code",
+ "client_id": _OAUTH_CLIENT_ID,
+ "code": code,
+ "state": received_state,
+ "redirect_uri": _OAUTH_REDIRECT_URI,
+ "code_verifier": verifier,
+ }).encode()
+
+ # Anthropic migrated the OAuth token endpoint to platform.claude.com;
+ # console.anthropic.com now 404s. Try the new host first, then fall
+ # back to console for older deployments (mirrors the refresh path).
+ # UA is _OAUTH_TOKEN_USER_AGENT (a non-claude-code UA) — see the
+ # constant's definition for why the token endpoint must not send
+ # claude-code/ (429 UA-prefix block).
+ result = None
+ last_error = None
+ for endpoint in _OAUTH_TOKEN_URLS:
+ req = urllib.request.Request(
+ endpoint,
+ data=exchange_data,
+ headers={
+ "Content-Type": "application/json",
+ "User-Agent": _OAUTH_TOKEN_USER_AGENT,
+ },
+ method="POST",
+ )
+ try:
+ with urllib.request.urlopen(req, timeout=15) as resp:
+ result = json.loads(resp.read().decode())
+ break
+ except Exception as exc:
+ last_error = exc
+ logger.debug("Anthropic token exchange failed at %s: %s", endpoint, exc)
+ continue
+
+ if result is None:
+ raise last_error if last_error is not None else ValueError(
+ "Anthropic token exchange failed"
+ )
+ except Exception as e:
+ print(f"Token exchange failed: {e}")
+ return None
+
+ access_token = result.get("access_token", "")
+ refresh_token = result.get("refresh_token", "")
+ expires_in = result.get("expires_in", 3600)
+
+ if not access_token:
+ print("No access token in response.")
+ return None
+
+ expires_at_ms = int(time.time() * 1000) + (expires_in * 1000)
+ return {
+ "access_token": access_token,
+ "refresh_token": refresh_token,
+ "expires_at_ms": expires_at_ms,
+ }
+
+
+def read_hermes_oauth_credentials() -> Optional[Dict[str, Any]]:
+ """Read Hermes-managed OAuth credentials from ~/.hermes/.anthropic_oauth.json."""
+ oauth_file = _get_hermes_oauth_file()
+ if oauth_file.exists():
+ try:
+ data = json.loads(oauth_file.read_text(encoding="utf-8"))
+ if data.get("accessToken"):
+ return data
+ except (json.JSONDecodeError, OSError, IOError) as e:
+ logger.debug("Failed to read Hermes OAuth credentials: %s", e)
+ return None
+
+
+def _write_hermes_oauth_credentials(
+ access_token: str,
+ refresh_token: Optional[str],
+ expires_at_ms: Optional[int],
+) -> None:
+ """Write refreshed hermes_pkce tokens back to ~/.hermes/.anthropic_oauth.json.
+
+ Without this, a successful pool-level refresh of a ``hermes_pkce``-sourced
+ entry is invisible to this singleton file. The next ``load_pool()`` call
+ runs ``_seed_from_singletons()``, which reads the stale file and
+ overwrites the freshly-rotated pool entry with the pre-refresh (and, for
+ single-use Anthropic refresh tokens, already-consumed) token pair.
+
+ Raises ``CredentialPersistError`` when the rotated pair does not reach the
+ file, for the same reason ``_write_claude_code_credentials`` does: this is
+ the commit step of the refresh transaction.
+ """
+ oauth_file = _get_hermes_oauth_file()
+ try:
+ oauth_data = {
+ "accessToken": access_token,
+ "refreshToken": refresh_token,
+ "expiresAt": expires_at_ms,
+ }
+ oauth_file.parent.mkdir(parents=True, exist_ok=True)
+ _tmp_oauth = oauth_file.with_suffix(f".tmp.{os.getpid()}.{secrets.token_hex(4)}")
+ try:
+ fd = os.open(
+ str(_tmp_oauth),
+ os.O_WRONLY | os.O_CREAT | os.O_EXCL,
+ stat.S_IRUSR | stat.S_IWUSR,
+ )
+ with os.fdopen(fd, "w", encoding="utf-8") as fh:
+ json.dump(oauth_data, fh, indent=2)
+ fh.flush()
+ os.fsync(fh.fileno())
+ os.replace(_tmp_oauth, oauth_file)
+ except OSError:
+ try:
+ _tmp_oauth.unlink(missing_ok=True)
+ except OSError:
+ pass
+ raise
+ except (OSError, IOError, ValueError) as e:
+ logger.error(
+ "Failed to write refreshed Hermes OAuth credentials to %s: %s", oauth_file, e
+ )
+ raise CredentialPersistError(oauth_file, e) from e
+
diff --git a/agent/anthropic_endpoints.py b/agent/anthropic_endpoints.py
new file mode 100644
index 00000000000..3cf3e10c622
--- /dev/null
+++ b/agent/anthropic_endpoints.py
@@ -0,0 +1,258 @@
+"""Endpoint-family detection for Anthropic-compatible base URLs.
+
+Hermes talks to a dozen services that speak the Anthropic Messages API but
+differ in auth style, accepted beta headers, and request quirks: MiniMax,
+Kimi/Moonshot, DeepSeek, OpenCode, Azure AI Foundry, the Nous portal, Bedrock.
+Every one of those differences is decided by inspecting the configured base
+URL, so the predicates live together here instead of being scattered through
+client construction and message conversion.
+
+Pure functions over a base-URL string - no I/O, no SDK, no credentials - which
+is what lets both ``agent/anthropic_adapter.py`` and
+``agent/anthropic_message_convert.py`` depend on this module without a cycle.
+
+``agent.anthropic_adapter`` re-exports every name below.
+"""
+
+from urllib.parse import urlparse
+
+from utils import base_url_host_matches, base_url_hostname
+
+
+def _normalize_base_url_text(base_url) -> str:
+ """Normalize SDK/base transport URL values to a plain string for inspection.
+
+ Some client objects expose ``base_url`` as an ``httpx.URL`` instead of a raw
+ string. Provider/auth detection should accept either shape.
+ """
+ if not base_url:
+ return ""
+ return str(base_url).strip()
+
+
+def _is_third_party_anthropic_endpoint(base_url: str | None) -> bool:
+ """Return True for non-Anthropic endpoints using the Anthropic Messages API.
+
+ Third-party proxies (Microsoft Foundry, AWS Bedrock, self-hosted) authenticate
+ with their own API keys via x-api-key, not Anthropic OAuth tokens. OAuth
+ detection should be skipped for these endpoints.
+ """
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False # No base_url = direct Anthropic API
+ normalized = normalized.rstrip("/").lower()
+ if "anthropic.com" in normalized:
+ return False # Direct Anthropic API — OAuth applies
+ return True # Any other endpoint is a third-party proxy
+
+
+def _is_kimi_coding_endpoint(base_url: str | None) -> bool:
+ """Return True for Kimi's /coding endpoint that requires claude-code UA."""
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False
+ return normalized.rstrip("/").lower().startswith("https://api.kimi.com/coding")
+
+
+def _is_opencode_endpoint(base_url: str | None) -> bool:
+ """Return True for OpenCode's Zen/Go relay (opencode.ai)."""
+ return base_url_host_matches(base_url or "", "opencode.ai")
+
+
+# Model-name prefixes that identify the Kimi / Moonshot family. Covers
+# - official slugs: ``kimi-k2.5``, ``kimi_thinking``, ``moonshot-v1-8k``
+# - common release lines: ``k1.5-...``, ``k2-thinking``, ``k25-...``, ``k2.5-...``,
+# and the bare Coding Plan slug ``k3`` (plus ``k3.x``/``k3-...`` variants)
+# Matched case-insensitively against the post-``normalize_model_name`` form,
+# so a caller's ``provider/vendor/model`` slug is handled the same as a
+# bare name.
+_KIMI_FAMILY_MODEL_PREFIXES = (
+ "kimi-", "kimi_",
+ "moonshot-", "moonshot_",
+ "k1.", "k1-",
+ "k2.", "k2-",
+ "k25", "k2.5",
+ "k3.", "k3-",
+)
+
+# Bare release slugs with no separator suffix (Kimi Coding Plan serves K3
+# as the exact slug ``k3``). Kept exact-match so unrelated model names that
+# merely start with the same characters don't get misclassified.
+_KIMI_FAMILY_EXACT_SLUGS = frozenset({"k3"})
+
+
+def _model_name_is_kimi_family(model: str | None) -> bool:
+ if not isinstance(model, str):
+ return False
+ m = model.strip().lower()
+ if not m:
+ return False
+ # Strip vendor prefix (e.g. ``moonshotai/kimi-k2.5`` → ``kimi-k2.5``)
+ if "/" in m:
+ m = m.rsplit("/", 1)[-1]
+ if m in _KIMI_FAMILY_EXACT_SLUGS:
+ return True
+ return m.startswith(_KIMI_FAMILY_MODEL_PREFIXES)
+
+
+def _is_kimi_family_endpoint(base_url: str | None, model: str | None = None) -> bool:
+ """Return True for any Kimi / Moonshot Anthropic-Messages-speaking endpoint.
+
+ Broader than ``_is_kimi_coding_endpoint`` — matches:
+
+ - Kimi's official ``/coding`` URL (legacy check, preserved)
+ - Any ``api.kimi.com`` / ``moonshot.ai`` / ``moonshot.cn`` host
+ - Custom or proxied endpoints whose *model* name is in the Kimi / Moonshot
+ family (``kimi-*``, ``moonshot-*``, ``k1.*``, ``k2.*``, …). Users with
+ ``api_mode: anthropic_messages`` on a private gateway fronting Kimi
+ fall into this branch — the upstream still enforces Kimi's thinking
+ semantics (reasoning_content required on every replayed tool-call
+ message) regardless of the gateway's hostname.
+
+ Used to decide whether to drop Anthropic's ``thinking`` kwarg and to
+ preserve unsigned reasoning_content-derived thinking blocks on replay.
+ See hermes-agent#13848, #17057.
+ """
+ if _is_kimi_coding_endpoint(base_url):
+ return True
+ for _domain in ("api.kimi.com", "moonshot.ai", "moonshot.cn"):
+ if base_url_host_matches(base_url or "", _domain):
+ return True
+ if _model_name_is_kimi_family(model):
+ return True
+ return False
+
+
+def _is_deepseek_anthropic_endpoint(base_url: str | None) -> bool:
+ """Return True for DeepSeek's Anthropic-compatible endpoint.
+
+ DeepSeek's ``/anthropic`` route speaks the Anthropic Messages protocol
+ but, when thinking mode is enabled, requires the ``thinking`` blocks
+ from prior assistant turns to round-trip on subsequent requests — the
+ generic third-party path strips them and triggers HTTP 400::
+
+ The content[].thinking in the thinking mode must be passed back
+ to the API.
+
+ Per DeepSeek's published compatibility matrix the blocks are unsigned
+ (no Anthropic-proprietary signature, no ``redacted_thinking`` support),
+ so this endpoint is handled with the same strip-signed / keep-unsigned
+ policy used for Kimi's ``/coding`` endpoint. The match is pinned to
+ the ``/anthropic`` path so the OpenAI-compatible ``api.deepseek.com``
+ base URL (which never reaches this adapter) is not misclassified.
+ See hermes-agent#16748.
+ """
+ if not base_url_host_matches(base_url or "", "api.deepseek.com"):
+ return False
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False
+ return "/anthropic" in normalized.rstrip("/").lower()
+
+
+def _is_nous_portal_endpoint(base_url: str | None) -> bool:
+ """Return True for Nous Portal's Anthropic Messages route.
+
+ Portal serves its ``anthropic/*`` catalog natively at
+ ``https://inference-api.nousresearch.com/v1/messages``. Portal-specific
+ behaviours key off this: Bearer JWT auth, verbatim catalog model ids,
+ and native thinking-signature replay.
+
+ Trusted hosts only:
+
+ 1. Prod hostname ``inference-api.nousresearch.com``
+ 2. The operator-set ``NOUS_INFERENCE_BASE_URL`` hostname (staging/preview)
+
+ Lookalikes such as ``inference-api.nousresearch.com.attacker.test`` are
+ rejected (hostname match, not substring).
+ """
+ if base_url_host_matches(base_url or "", "inference-api.nousresearch.com"):
+ return True
+ try:
+ from hermes_cli.auth import _nous_inference_env_override
+
+ override = _nous_inference_env_override()
+ except Exception:
+ return False
+ if not override:
+ return False
+ # Exact host equality (not subdomain) so the env override can't broaden
+ # into sibling hosts the operator did not set.
+ override_host = base_url_hostname(override)
+ return bool(override_host) and base_url_hostname(base_url or "") == override_host
+
+
+def _requires_bearer_auth(base_url: str | None) -> bool:
+ """Return True for Anthropic-compatible providers that require Bearer auth.
+
+ Some third-party /anthropic endpoints implement Anthropic's Messages API but
+ require Authorization: Bearer instead of Anthropic's native x-api-key header.
+ MiniMax's global and China Anthropic-compatible endpoints, Azure AI
+ Foundry's Anthropic-style endpoint, Palantir Foundry's LLM proxy, and Nous
+ Portal's Messages route follow this pattern.
+ """
+ if _is_nous_portal_endpoint(base_url):
+ return True
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False
+ normalized = normalized.rstrip("/").lower()
+ return (
+ normalized.startswith(("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic"))
+ or "azure.com" in normalized
+ # Palantir Foundry LLM proxy (<org>.palantirfoundry.com/api/v2/llm/proxy/anthropic)
+ # rejects x-api-key with 401 and requires Authorization: Bearer.
+ # Hostname match (not substring) so e.g. evil.com/palantirfoundry
+ # paths don't trigger Bearer auth.
+ or base_url_host_matches(normalized, "palantirfoundry.com")
+ # CommandCode's /provider/v1/messages endpoint uses Bearer auth,
+ # not Anthropic's native x-api-key header. Hostname match for the
+ # same reason as above.
+ or base_url_host_matches(normalized, "api.commandcode.ai")
+ )
+
+
+def _base_url_needs_context_1m_beta(base_url: str | None) -> bool:
+ """Return True for endpoints that still gate 1M context behind a beta."""
+ normalized = _normalize_base_url_text(base_url).lower()
+ if not normalized:
+ return False
+ return "azure.com" in normalized
+
+
+def _is_minimax_anthropic_endpoint(base_url: str | None) -> bool:
+ """Return True for MiniMax's Anthropic-compatible endpoints.
+
+ MiniMax rejects the fine-grained-tool-streaming and context-1m betas;
+ those need to be stripped even though MiniMax also uses Bearer auth.
+ """
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False
+ normalized = normalized.rstrip("/").lower()
+ return normalized.startswith(
+ ("https://api.minimax.io/anthropic", "https://api.minimaxi.com/anthropic")
+ )
+
+
+def _is_azure_anthropic_endpoint(base_url: str | None) -> bool:
+ """Return True for Azure-hosted Anthropic Messages endpoints.
+
+ Covers both the modern Foundry host family (``*.services.ai.azure.*``)
+ and the legacy Azure OpenAI host family (``*.openai.azure.*``) when
+ serving Anthropic's ``/anthropic`` route. Used to opt-in those hosts
+ to the ``api-version`` query-param plumbing required by Azure.
+
+ Intentionally avoids a finite allow-list of TLD suffixes so it works
+ across sovereign / private Azure clouds.
+ """
+ normalized = _normalize_base_url_text(base_url)
+ if not normalized:
+ return False
+ parsed = urlparse(normalized)
+ host = (parsed.hostname or "").lower().rstrip(".")
+ path = (parsed.path or "").lower()
+ host_padded = f".{host}."
+ is_foundry_host = ".services.ai.azure." in host_padded
+ is_legacy_azoai_host = ".openai.azure." in host_padded
+ return (is_foundry_host or is_legacy_azoai_host) and "/anthropic" in path
diff --git a/agent/anthropic_message_convert.py b/agent/anthropic_message_convert.py
new file mode 100644
index 00000000000..4ef3e887cb6
--- /dev/null
+++ b/agent/anthropic_message_convert.py
@@ -0,0 +1,1225 @@
+"""OpenAI-style -> Anthropic Messages API request conversion.
+
+Everything here rewrites *request payloads*: model-id normalization, tool
+schemas, and the message list (content blocks, thinking blocks and their
+signatures, tool_use/tool_result pairing, cache_control placement, screenshot
+eviction, blank-block scrubbing).
+
+Split out of ``agent/anthropic_adapter.py`` so the adapter keeps client
+construction and the API call itself, while the payload-shaping rules - by far
+the largest and most fiddly part - have their own home. The endpoint-family
+predicates a few of these rules branch on come from
+``agent/anthropic_endpoints.py``, so this module never imports the adapter and
+there is no import cycle.
+
+``agent.anthropic_adapter`` re-exports every name below, so existing
+``from agent.anthropic_adapter import convert_messages_to_anthropic`` imports
+keep working.
+"""
+
+import copy
+import json
+import logging
+from typing import Any, Dict, List, Optional, Tuple
+
+from agent.anthropic_endpoints import (
+ _is_deepseek_anthropic_endpoint,
+ _is_kimi_family_endpoint,
+ _is_nous_portal_endpoint,
+ _is_third_party_anthropic_endpoint,
+)
+
+logger = logging.getLogger(__name__)
+
+
+# ---------------------------------------------------------------------------
+# Message / tool / response format conversion
+# ---------------------------------------------------------------------------
+
+
+def _is_bedrock_model_id(model: str) -> bool:
+ """Detect AWS Bedrock model IDs that use dots as namespace separators.
+
+ Bedrock model IDs come in two forms:
+ - Bare: ``anthropic.claude-opus-4-7``
+ - Regional (inference profiles): ``us.anthropic.claude-sonnet-4-5-v1:0``
+
+ In both cases the dots separate namespace components, not version
+ numbers, and must be preserved verbatim for the Bedrock API.
+ """
+ lower = model.lower()
+ # Regional inference-profile prefixes
+ if any(lower.startswith(p) for p in (
+ "global.", "us.", "eu.", "apac.", "ap.", "au.", "jp.",
+ "ca.", "sa.", "me.", "af.",
+ )):
+ return True
+ # Bare Bedrock model IDs: provider.model-family
+ if lower.startswith("anthropic."):
+ return True
+ return False
+
+
+def normalize_model_name(model: str, preserve_dots: bool = False) -> str:
+ """Normalize a model name for the Anthropic API.
+
+ - Strips 'anthropic/' prefix (OpenRouter format, case-insensitive)
+ - Converts dots to hyphens in version numbers (OpenRouter uses dots,
+ Anthropic uses hyphens: claude-opus-4.6 → claude-opus-4-6), unless
+ preserve_dots is True (e.g. for Alibaba/DashScope: qwen3.5-plus).
+ - Preserves Bedrock model IDs (``anthropic.claude-opus-4-7``) and
+ regional inference profiles (``us.anthropic.claude-*``) whose dots
+ are namespace separators, not version separators.
+ """
+ lower = model.lower()
+ if lower.startswith("anthropic/"):
+ model = model[len("anthropic/"):]
+ if not preserve_dots:
+ # Bedrock model IDs use dots as namespace separators
+ # (e.g. "anthropic.claude-opus-4-7", "us.anthropic.claude-*").
+ # These must not be converted to hyphens. See issue #12295.
+ if _is_bedrock_model_id(model):
+ return model
+ # Only convert dots to hyphens for Anthropic/Claude models.
+ # Non-Anthropic models (gpt-5.4, gemini-2.5, etc.) use dots
+ # as part of their canonical names. See issue #17171.
+ _lower = model.lower()
+ if _lower.startswith("claude-") or _lower.startswith("anthropic/"):
+ model = model.replace(".", "-")
+ return model
+
+
+def _sanitize_tool_id(tool_id: str) -> str:
+ """Sanitize a tool call ID for the Anthropic API.
+
+ Anthropic requires IDs matching [a-zA-Z0-9_-]. Replace invalid
+ characters with underscores and ensure non-empty.
+ """
+ import re
+ if not tool_id:
+ return "tool_0"
+ sanitized = re.sub(r"[^a-zA-Z0-9_-]", "_", tool_id)
+ return sanitized or "tool_0"
+
+
+def _normalize_tool_input_schema(schema: Any) -> Dict[str, Any]:
+ """Normalize tool schemas before sending them to Anthropic.
+
+ Anthropic's tool schema validator rejects nullable unions such as
+ ``anyOf: [{"type": "string"}, {"type": "null"}]`` that Pydantic/MCP
+ commonly emits for optional fields. Tool optionality is represented by
+ the parent ``required`` array, so we delegate to the shared
+ ``strip_nullable_unions`` helper to collapse nullable unions to the
+ non-null branch while preserving metadata like description/default.
+
+ ``keep_nullable_hint=False`` because the Anthropic validator does not
+ recognize the OpenAPI-style ``nullable: true`` extension and strict
+ schema-to-grammar converters may reject unknown keywords.
+
+ Top-level ``oneOf``/``allOf``/``anyOf`` are also stripped here: the
+ Anthropic API rejects union keywords at the schema root with a generic
+ HTTP 400. Several upstream and plugin tools ship schemas with one of
+ these keywords at the top level (commonly for Pydantic discriminated
+ unions). If we land here with those keywords still present after
+ nullable-union stripping, drop them and fall back to a plain object
+ schema so the tool still validates at the Anthropic boundary.
+ """
+ if not schema:
+ return {"type": "object", "properties": {}}
+
+ from tools.schema_sanitizer import strip_nullable_unions
+
+ normalized = strip_nullable_unions(schema, keep_nullable_hint=False)
+ if not isinstance(normalized, dict):
+ return {"type": "object", "properties": {}}
+ # Strip top-level union keywords that Anthropic's validator rejects.
+ banned = {"oneOf", "allOf", "anyOf"}
+ if banned & normalized.keys():
+ normalized = {k: v for k, v in normalized.items() if k not in banned}
+ if "type" not in normalized:
+ normalized["type"] = "object"
+ if normalized.get("type") == "object" and not isinstance(normalized.get("properties"), dict):
+ normalized = {**normalized, "properties": {}}
+ return normalized
+
+
+def convert_tools_to_anthropic(tools: List[Dict]) -> List[Dict]:
+ """Convert OpenAI tool definitions to Anthropic format."""
+ if not tools:
+ return []
+ result = []
+ seen_names: set = set()
+ for t in tools:
+ fn = t.get("function", {})
+ name = fn.get("name", "")
+ # Defensive dedup: Anthropic rejects requests with duplicate tool
+ # names. Upstream injection paths already dedup, but this guard
+ # converts a hard API failure into a warning. See: #18478
+ if name and name in seen_names:
+ logger.warning(
+ "convert_tools_to_anthropic: duplicate tool name '%s' "
+ "— dropping second occurrence",
+ name,
+ )
+ continue
+ if name:
+ seen_names.add(name)
+ anthropic_tool: Dict[str, Any] = {
+ "name": name,
+ "description": fn.get("description", ""),
+ "input_schema": _normalize_tool_input_schema(
+ fn.get("parameters", {"type": "object", "properties": {}})
+ ),
+ }
+ # Forward cache_control marker when present on the OpenAI-format
+ # tool dict. Anthropic's tools array supports cache_control on the
+ # last tool to cache the entire schema cross-session.
+ cache_control = t.get("cache_control")
+ if isinstance(cache_control, dict):
+ anthropic_tool["cache_control"] = dict(cache_control)
+ result.append(anthropic_tool)
+ return result
+
+
+def _image_source_from_openai_url(url: str) -> Dict[str, str]:
+ """Convert an OpenAI-style image URL/data URL into Anthropic image source."""
+ url = str(url or "").strip()
+ if not url:
+ return {"type": "url", "url": ""}
+
+ if url.startswith("data:"):
+ header, _, data = url.partition(",")
+ media_type = "image/jpeg"
+ if header.startswith("data:"):
+ mime_part = header[len("data:"):].split(";", 1)[0].strip()
+ if mime_part.startswith("image/"):
+ media_type = mime_part
+ return {
+ "type": "base64",
+ "media_type": media_type,
+ "data": data,
+ }
+
+ return {"type": "url", "url": url}
+
+
+def _convert_content_part_to_anthropic(part: Any) -> Optional[Dict[str, Any]]:
+ """Convert a single OpenAI-style content part to Anthropic format."""
+ if part is None:
+ return None
+ if isinstance(part, str):
+ return {"type": "text", "text": part}
+ if not isinstance(part, dict):
+ return {"type": "text", "text": str(part)}
+
+ ptype = part.get("type")
+
+ if ptype == "input_text":
+ block: Dict[str, Any] = {"type": "text", "text": part.get("text", "")}
+ elif ptype == "text":
+ # A stored Anthropic text block. Rebuild from whitelisted fields only —
+ # SDK response text blocks carry output-only siblings (parsed_output,
+ # citations=None) that the Messages INPUT schema rejects with HTTP 400
+ # "Extra inputs are not permitted". Do NOT dict(part) it verbatim.
+ block = {"type": "text", "text": part.get("text", "")}
+ cits = part.get("citations")
+ if isinstance(cits, list) and cits:
+ block["citations"] = cits
+ elif ptype in {"image_url", "input_image"}:
+ image_value = part.get("image_url", {})
+ url = image_value.get("url", "") if isinstance(image_value, dict) else str(image_value or "")
+ block = {"type": "image", "source": _image_source_from_openai_url(url)}
+ else:
+ block = dict(part)
+
+ if isinstance(part.get("cache_control"), dict) and "cache_control" not in block:
+ block["cache_control"] = dict(part["cache_control"])
+ return block
+
+
+def _to_plain_data(value: Any, *, _depth: int = 0, _path: Optional[set] = None) -> Any:
+ """Recursively convert SDK objects to plain Python data structures.
+
+ Guards against circular references (``_path`` tracks ``id()`` of objects
+ on the *current* recursion path) and runaway depth (capped at 20 levels).
+ Uses path-based tracking so shared (but non-cyclic) objects referenced by
+ multiple siblings are converted correctly rather than being stringified.
+ """
+ _MAX_DEPTH = 20
+ if _depth > _MAX_DEPTH:
+ return str(value)
+
+ if _path is None:
+ _path = set()
+
+ obj_id = id(value)
+ if obj_id in _path:
+ return str(value)
+
+ if hasattr(value, "model_dump"):
+ _path.add(obj_id)
+ try:
+ # warnings=False: content blocks from the streaming accumulator
+ # (ParsedTextBlock et al.) trip pydantic's serializer-mismatch
+ # UserWarning against the generic Message union; the dump itself
+ # is correct, and the warning leaks to the user's terminal.
+ dumped = value.model_dump(warnings=False)
+ except TypeError:
+ # Duck-typed model_dump without pydantic's signature.
+ dumped = value.model_dump()
+ result = _to_plain_data(dumped, _depth=_depth + 1, _path=_path)
+ _path.discard(obj_id)
+ return result
+ if isinstance(value, dict):
+ _path.add(obj_id)
+ result = {k: _to_plain_data(v, _depth=_depth + 1, _path=_path) for k, v in value.items()}
+ _path.discard(obj_id)
+ return result
+ if isinstance(value, (list, tuple)):
+ _path.add(obj_id)
+ result = [_to_plain_data(v, _depth=_depth + 1, _path=_path) for v in value]
+ _path.discard(obj_id)
+ return result
+ if hasattr(value, "__dict__"):
+ _path.add(obj_id)
+ result = {
+ k: _to_plain_data(v, _depth=_depth + 1, _path=_path)
+ for k, v in vars(value).items()
+ if not k.startswith("_")
+ }
+ _path.discard(obj_id)
+ return result
+ return value
+
+
+def _extract_preserved_thinking_blocks(message: Dict[str, Any]) -> List[Dict[str, Any]]:
+ """Return Anthropic thinking blocks previously preserved on the message."""
+ raw_details = message.get("reasoning_details")
+ if not isinstance(raw_details, list):
+ return []
+
+ preserved: List[Dict[str, Any]] = []
+ for detail in raw_details:
+ if not isinstance(detail, dict):
+ continue
+ block_type = str(detail.get("type", "") or "").strip().lower()
+ if block_type not in {"thinking", "redacted_thinking"}:
+ continue
+ preserved.append(copy.deepcopy(detail))
+ return preserved
+
+
+def _convert_content_to_anthropic(content: Any) -> Any:
+ """Convert OpenAI-style multimodal content arrays to Anthropic blocks."""
+ if not isinstance(content, list):
+ return content
+
+ converted = []
+ for part in content:
+ block = _convert_content_part_to_anthropic(part)
+ if block is not None:
+ converted.append(block)
+ return converted
+
+
+def _content_parts_to_anthropic_blocks(parts: Any) -> List[Dict[str, Any]]:
+ """Convert OpenAI-style tool-message content parts → Anthropic tool_result inner blocks.
+
+ Used for multimodal tool results (e.g. computer_use screenshots). Each
+ part is normalized via `_convert_content_part_to_anthropic`, then
+ filtered to the block types Anthropic tool_result accepts (text + image).
+ """
+ if not isinstance(parts, list):
+ return []
+ out: List[Dict[str, Any]] = []
+ for part in parts:
+ block = _convert_content_part_to_anthropic(part)
+ if not block:
+ continue
+ btype = block.get("type")
+ if btype == "text":
+ text_val = block.get("text")
+ if isinstance(text_val, str) and text_val:
+ out.append({"type": "text", "text": text_val})
+ elif btype == "image":
+ src = block.get("source")
+ if isinstance(src, dict) and src:
+ out.append({"type": "image", "source": src})
+ return out
+
+
+_EMPTY_TEXT_PLACEHOLDER = "(empty)"
+
+
+def _safe_text(text: Any) -> str:
+ """Return ``text`` if it's non-whitespace, else a non-whitespace placeholder.
+
+ The Anthropic Messages API rejects requests where a text content block is
+ empty or whitespace-only (HTTP 400 "text content blocks must contain
+ non-whitespace text"). When such a block gets stored in session history —
+ e.g. produced by context compression — it is replayed verbatim on every
+ subsequent turn, permanently wedging the session. Coercing to a
+ non-whitespace placeholder is self-healing: the next API call recovers.
+
+ Mirrors ``bedrock_adapter._safe_text`` (#9486); ref #69512.
+ """
+ if text is None:
+ return _EMPTY_TEXT_PLACEHOLDER
+ if not isinstance(text, str):
+ text = str(text)
+ return text if text.strip() else _EMPTY_TEXT_PLACEHOLDER
+
+
+def _sanitize_replay_block(b: Dict[str, Any]) -> Optional[Dict[str, Any]]:
+ """Strip output-only fields from a stored Anthropic content block so it is
+ valid as REQUEST input on replay.
+
+ The SDK response objects carry output-only attributes that the Messages
+ *input* schema forbids ("Extra inputs are not permitted"): text blocks get
+ ``parsed_output``/``citations`` (when null), tool_use blocks get ``caller``,
+ etc. ``normalize_response`` captured blocks verbatim via ``_to_plain_data``,
+ so these leak back as input on the next turn → HTTP 400.
+
+ Whitelist per type (NOT a blacklist) so future SDK output-only fields can't
+ reintroduce the bug. Returns a clean block, or None to drop it.
+ """
+ if not isinstance(b, dict):
+ return None
+ btype = b.get("type")
+ if btype == "text":
+ text_val = b.get("text", "")
+ # Bedrock and strict Anthropic-compatible endpoints reject text
+ # blocks where "text" is empty or whitespace-only (#69512). Drop the
+ # blank block (the caller relocates any cache_control it carried and
+ # falls back to a non-whitespace placeholder when nothing survives)
+ # rather than coercing in place — a coerced "(empty)" block would be
+ # model-visible noise next to surviving thinking/tool_use blocks.
+ # Type-safe: captured blocks can carry text=None from an invalid
+ # upstream payload, which a bare .strip() would crash on.
+ if not isinstance(text_val, str) or not text_val.strip():
+ return None
+ out: Dict[str, Any] = {"type": "text", "text": text_val}
+ # citations is input-valid ONLY when it's a non-empty list; the SDK
+ # emits citations=None on responses, which the input schema rejects.
+ cits = b.get("citations")
+ if isinstance(cits, list) and cits:
+ out["citations"] = cits
+ if isinstance(b.get("cache_control"), dict):
+ out["cache_control"] = b["cache_control"]
+ return out
+ if btype == "thinking":
+ out = {"type": "thinking", "thinking": b.get("thinking", "")}
+ if b.get("signature"):
+ out["signature"] = b["signature"]
+ return out
+ if btype == "redacted_thinking":
+ # Only valid with its data payload; drop if missing.
+ return {"type": "redacted_thinking", "data": b["data"]} if b.get("data") else None
+ if btype == "tool_use":
+ out = {
+ "type": "tool_use",
+ "id": _sanitize_tool_id(b.get("id", "")),
+ "name": b.get("name", ""),
+ "input": b.get("input", {}),
+ }
+ if isinstance(b.get("cache_control"), dict):
+ out["cache_control"] = b["cache_control"]
+ return out
+ if btype == "image":
+ src = b.get("source")
+ return {"type": "image", "source": src} if isinstance(src, dict) else None
+ # Unknown/unsupported block type on the input path — drop rather than risk
+ # another "Extra inputs are not permitted".
+ return None
+
+
+def _apply_assistant_cache_control_to_last_cacheable_block(
+ blocks: List[Dict[str, Any]],
+ cache_control: Any,
+) -> None:
+ if not isinstance(cache_control, dict):
+ return
+ for block in reversed(blocks):
+ if isinstance(block, dict) and block.get("type") in {"text", "tool_use"}:
+ block.setdefault("cache_control", dict(cache_control))
+ break
+
+
+def _convert_assistant_message(m: Dict[str, Any]) -> Dict[str, Any]:
+ """Convert an assistant message to Anthropic content blocks.
+
+ Handles thinking blocks, regular content, tool calls, and
+ reasoning_content injection for Kimi/DeepSeek endpoints.
+ """
+ content = m.get("content", "")
+ # Anthropic interleaved-thinking fast path: when this turn carries a
+ # verbatim, order-preserving block list (set by normalize_response only
+ # for turns that interleave SIGNED thinking with tool_use), replay it.
+ # Each block is run through _sanitize_replay_block to strip output-only
+ # SDK fields (parsed_output, caller, citations=None, …) that the Messages
+ # INPUT schema forbids — replaying them verbatim caused HTTP 400 "Extra
+ # inputs are not permitted" (text.parsed_output). Block ORDER is preserved
+ # (the reason this channel exists); only forbidden sibling fields are
+ # dropped, leaving thinking signatures and tool_use id/name/input intact.
+ ordered_blocks = m.get("anthropic_content_blocks")
+ if isinstance(ordered_blocks, list) and ordered_blocks:
+ # Re-source each tool_use input from the stored tool_calls map rather
+ # than the captured block. The ordered-blocks list captures tool_use
+ # input from the RAW API response (normalize_response), which is NOT
+ # credential-redacted; tool_calls[].function.arguments IS redacted at
+ # storage time (build_assistant_message, #19798). Replaying the raw
+ # block input would resurrect a secret the model inlined into a tool
+ # call (e.g. terminal(command="curl -H 'Authorization: Bearer sk-...'")
+ # onto the wire, even though the same value is redacted everywhere else
+ # in history. Keying by sanitized tool id preserves interleave order
+ # (the reason this channel exists) while swapping in the redacted
+ # input. Adapted from #36071 (replay-time tool-input re-sourcing).
+ redacted_input_by_id: Dict[str, Any] = {}
+ for tc in m.get("tool_calls", []) or []:
+ if not isinstance(tc, dict):
+ continue
+ fn = tc.get("function", {}) or {}
+ raw_args = fn.get("arguments", "{}")
+ try:
+ parsed_args = json.loads(raw_args) if isinstance(raw_args, str) else raw_args
+ except (json.JSONDecodeError, ValueError):
+ parsed_args = {}
+ redacted_input_by_id[_sanitize_tool_id(tc.get("id", ""))] = parsed_args
+ replayed: List[Dict[str, Any]] = []
+ _relocated_replay_cache_control = None
+ _dropped_blank_text = False
+ for b in ordered_blocks:
+ clean = _sanitize_replay_block(b)
+ if clean is None:
+ if isinstance(b, dict) and b.get("type") == "text":
+ _dropped_blank_text = True
+ if isinstance(b, dict) and isinstance(b.get("cache_control"), dict):
+ # A dropped blank text block can still carry the cache
+ # breakpoint marker -- relocate it rather than losing it.
+ _relocated_replay_cache_control = b["cache_control"]
+ continue
+ if clean.get("type") == "tool_use":
+ # Override raw (un-redacted) input with the redacted copy when
+ # we have one for this id; fall back to the sanitized block
+ # input only if the tool_call is missing (shape mismatch).
+ redacted = redacted_input_by_id.get(clean.get("id", ""))
+ if redacted is not None:
+ clean["input"] = redacted
+ replayed.append(clean)
+ # When every text block was blank and nothing cacheable survived
+ # (e.g. signed thinking + a blank text block, or a SOLE blank
+ # cache-marked block), emit the non-whitespace placeholder so the
+ # replayed message stays schema-valid (#69512) and a relocated cache
+ # marker still has a carrier instead of being silently lost.
+ _has_cacheable_replay = any(
+ isinstance(b, dict) and b.get("type") in {"text", "tool_use"}
+ for b in replayed
+ )
+ if not _has_cacheable_replay and (
+ _dropped_blank_text or _relocated_replay_cache_control is not None
+ ):
+ replayed.append({"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER})
+ if replayed:
+ if _relocated_replay_cache_control is not None:
+ _apply_assistant_cache_control_to_last_cacheable_block(
+ replayed, _relocated_replay_cache_control
+ )
+ _apply_assistant_cache_control_to_last_cacheable_block(
+ replayed, m.get("cache_control")
+ )
+ # apply_anthropic_cache_control marks an assistant turn with
+ # non-empty text by writing cache_control INTO ``content`` (see
+ # _apply_cache_marker's list branch), not at the top level. This
+ # branch rebuilds the message from ordered_blocks and never reads
+ # ``content``, so that marker would be dropped -- and because
+ # _can_carry_marker already counted this message as a carrier, the
+ # breakpoint is burned rather than relocated. #56195 covered the
+ # complementary shape (blank content -> top-level marker); this is
+ # the interleaved thinking + preamble-text + tool_use shape.
+ _inline_cc = None
+ _msg_content = m.get("content")
+ if isinstance(_msg_content, list):
+ for _blk in _msg_content:
+ if isinstance(_blk, dict) and isinstance(
+ _blk.get("cache_control"), dict
+ ):
+ _inline_cc = _blk["cache_control"]
+ break
+ if _inline_cc is not None:
+ _apply_assistant_cache_control_to_last_cacheable_block(
+ replayed, _inline_cc
+ )
+ return {"role": "assistant", "content": replayed}
+
+ blocks = _extract_preserved_thinking_blocks(m)
+ # Cache markers dropped along with a blank block are relocated onto the
+ # last surviving cacheable block below (via
+ # _apply_assistant_cache_control_to_last_cacheable_block), rather than
+ # lost -- prompt_caching.py's _apply_cache_marker() sets cache_control
+ # directly on content[-1] for list content, so if that last part happens
+ # to be blank text, dropping it silently would lose the breakpoint.
+ _relocated_cache_control = None
+ if content:
+ if isinstance(content, list):
+ converted_content = _convert_content_to_anthropic(content)
+ if isinstance(converted_content, list):
+ # Bedrock and strict Anthropic-compatible endpoints reject
+ # text blocks where "text" is empty or whitespace-only. The
+ # ordered-replay path enforces the same invariant via
+ # _sanitize_replay_block(). Type-safe against ANY invalid
+ # "text" value from an upstream payload -- None, or a
+ # truthy non-string like an int -- not just None: checking
+ # isinstance() first (rather than `blk.get("text") or ""`)
+ # means a non-string value is treated as blank/invalid
+ # instead of reaching .strip() and raising AttributeError.
+ for blk in converted_content:
+ _blk_text = blk.get("text") if isinstance(blk, dict) else None
+ if (
+ isinstance(blk, dict)
+ and blk.get("type") == "text"
+ and (not isinstance(_blk_text, str) or not _blk_text.strip())
+ ):
+ if isinstance(blk.get("cache_control"), dict):
+ _relocated_cache_control = blk["cache_control"]
+ continue
+ blocks.append(blk)
+ else:
+ # Scalar (non-list) content: a whitespace-only string is the
+ # same invalid-payload case as an empty list block -- drop it
+ # rather than emitting a blank text block.
+ text_str = str(content)
+ if text_str.strip():
+ blocks.append({"type": "text", "text": text_str})
+ for tc in m.get("tool_calls", []):
+ if not tc or not isinstance(tc, dict):
+ continue
+ fn = tc.get("function", {})
+ args = fn.get("arguments", "{}")
+ try:
+ parsed_args = json.loads(args) if isinstance(args, str) else args
+ except (json.JSONDecodeError, ValueError):
+ parsed_args = {}
+ blocks.append({
+ "type": "tool_use",
+ "id": _sanitize_tool_id(tc.get("id", "")),
+ "name": fn.get("name", ""),
+ "input": parsed_args,
+ })
+ # Kimi's /coding endpoint (Anthropic protocol) requires assistant
+ # tool-call messages to carry reasoning_content when thinking is
+ # enabled server-side. Preserve it as a thinking block so Kimi
+ # can validate the message history. See hermes-agent#13848.
+ #
+ # Accept empty string "" — _copy_reasoning_content_for_api()
+ # injects "" as a tier-3 fallback for Kimi tool-call messages
+ # that had no reasoning. Kimi requires the field to exist, even
+ # if empty.
+ #
+ # Prepend (not append): Anthropic protocol requires thinking
+ # blocks before text and tool_use blocks.
+ #
+ # Guard: only add when reasoning_details didn't already contribute
+ # thinking blocks. On native Anthropic, reasoning_details produces
+ # signed thinking blocks — adding another unsigned one from
+ # reasoning_content would create a duplicate (same text) that gets
+ # downgraded to a spurious text block on the last assistant message.
+ reasoning_content = m.get("reasoning_content")
+ _already_has_thinking = any(
+ isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"}
+ for b in blocks
+ )
+ if isinstance(reasoning_content, str) and not _already_has_thinking:
+ blocks.insert(0, {"type": "thinking", "thinking": reasoning_content})
+ # Anthropic rejects empty assistant content. IMPORTANT: fall back only
+ # to the placeholder, never to the raw `content` variable -- `content`
+ # is the UNFILTERED original message content, and can itself be exactly
+ # the blank/whitespace-only payload the filtering above just removed
+ # (a sole blank text block, or scalar whitespace with no tool_calls).
+ # `blocks or content` there would silently restore the invalid provider
+ # payload this function exists to prevent (#69512).
+ effective = blocks if blocks else [{"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER}]
+ # Applied here (after the empty-fallback resolution) rather than
+ # earlier against `blocks` directly, so a cache_control relocated from
+ # a dropped blank block that was the ONLY block still lands on the
+ # (empty) placeholder instead of being silently lost when blocks was
+ # empty at the point the marker would otherwise have been applied.
+ if _relocated_cache_control is not None:
+ _apply_assistant_cache_control_to_last_cacheable_block(
+ effective, _relocated_cache_control
+ )
+ _apply_assistant_cache_control_to_last_cacheable_block(
+ effective, m.get("cache_control")
+ )
+ return {"role": "assistant", "content": effective}
+
+
+def _convert_tool_message_to_result(
+ result: List[Dict[str, Any]], m: Dict[str, Any]
+) -> None:
+ """Convert a tool message to an Anthropic tool_result, merging consecutive
+ results into one user message.
+
+ Mutates ``result`` in place — either appends a new user message or extends
+ the trailing user message's tool_result list.
+ """
+ content = m.get("content", "")
+ multimodal_blocks: Optional[List[Dict[str, Any]]] = None
+ if isinstance(content, dict) and content.get("_multimodal"):
+ multimodal_blocks = _content_parts_to_anthropic_blocks(
+ content.get("content") or []
+ )
+ # Fallback text if the conversion produced nothing usable.
+ if not multimodal_blocks and content.get("text_summary"):
+ multimodal_blocks = [
+ {"type": "text", "text": str(content["text_summary"])}
+ ]
+ elif isinstance(content, list):
+ converted = _content_parts_to_anthropic_blocks(content)
+ if any(b.get("type") == "image" for b in converted):
+ multimodal_blocks = converted
+ # Back-compat: some callers stash blocks under a private key.
+ if multimodal_blocks is None:
+ stashed = m.get("_anthropic_content_blocks")
+ if isinstance(stashed, list) and stashed:
+ text_content = content if isinstance(content, str) and content.strip() else None
+ multimodal_blocks = (
+ [{"type": "text", "text": text_content}] + stashed
+ if text_content else list(stashed)
+ )
+
+ if multimodal_blocks:
+ result_content: Any = multimodal_blocks
+ elif isinstance(content, str):
+ result_content = content
+ else:
+ result_content = json.dumps(content) if content else "(no output)"
+ if not result_content:
+ result_content = "(no output)"
+ tool_result = {
+ "type": "tool_result",
+ "tool_use_id": _sanitize_tool_id(m.get("tool_call_id", "")),
+ "content": result_content,
+ }
+ if isinstance(m.get("cache_control"), dict):
+ tool_result["cache_control"] = dict(m["cache_control"])
+ # Merge consecutive tool results into one user message
+ if (
+ result
+ and result[-1]["role"] == "user"
+ and isinstance(result[-1]["content"], list)
+ and result[-1]["content"]
+ and result[-1]["content"][0].get("type") == "tool_result"
+ ):
+ result[-1]["content"].append(tool_result)
+ else:
+ result.append({"role": "user", "content": [tool_result]})
+
+
+def _convert_user_message(content: Any) -> Dict[str, Any]:
+ """Validate and convert a user message to anthropic format."""
+ if isinstance(content, list):
+ converted_blocks = _convert_content_to_anthropic(content)
+ kept_blocks = _fix_blank_text_blocks_in_list(
+ converted_blocks,
+ placeholder_text="(empty message)",
+ msg_index=-1,
+ role="user",
+ location="_convert_user_message",
+ )
+ return {"role": "user", "content": kept_blocks}
+ else:
+ if not content or (isinstance(content, str) and not content.strip()):
+ content = "(empty message)"
+ return {"role": "user", "content": content}
+
+
+def _strip_orphaned_tool_blocks(result: List[Dict[str, Any]]) -> None:
+ """Strip tool_use blocks with no matching tool_result, and vice versa.
+
+ Context compression or session truncation can remove either side of a
+ tool-call pair, or insert messages between a tool_use and its result.
+ Anthropic requires each tool_use to have a matching tool_result in the
+ IMMEDIATELY FOLLOWING user message — a global ID match is not enough.
+ Mutates ``result`` in place.
+ """
+ # Pass 1: For each assistant message with tool_use blocks, check that
+ # EACH tool_use ID has a matching tool_result in the immediately following
+ # user message. Strip tool_use blocks that lack an adjacent result —
+ # Anthropic rejects non-adjacent pairs with HTTP 400 even when the IDs
+ # match somewhere later in the conversation.
+ for i, m in enumerate(result):
+ if m.get("role") != "assistant" or not isinstance(m.get("content"), list):
+ continue
+ tool_use_ids_in_turn = {
+ b.get("id")
+ for b in m["content"]
+ if isinstance(b, dict) and b.get("type") == "tool_use"
+ }
+ if not tool_use_ids_in_turn:
+ continue
+
+ # Collect result IDs from the immediately following user message only.
+ adjacent_result_ids: set = set()
+ if i + 1 < len(result):
+ nxt = result[i + 1]
+ if nxt.get("role") == "user" and isinstance(nxt.get("content"), list):
+ for block in nxt["content"]:
+ if isinstance(block, dict) and block.get("type") == "tool_result":
+ adjacent_result_ids.add(block.get("tool_use_id"))
+
+ orphaned = tool_use_ids_in_turn - adjacent_result_ids
+ if not orphaned:
+ continue
+
+ kept = [
+ b
+ for b in m["content"]
+ if not (isinstance(b, dict) and b.get("type") == "tool_use" and b.get("id") in orphaned)
+ ]
+ # If stripping an orphaned tool_use mutated a turn that also carries a
+ # signed thinking block, that block's Anthropic signature was computed
+ # against the ORIGINAL (un-stripped) turn content and is now invalid.
+ # Anthropic rejects the replayed turn with HTTP 400 "thinking blocks in
+ # the latest assistant message cannot be modified". Flag the turn so
+ # _manage_thinking_signatures can demote the dead signature instead of
+ # replaying it verbatim. See hermes-agent: extended-thinking + parallel
+ # tool batch interrupted mid-flight → non-retryable 400 crash-loop.
+ if len(kept) != len(m["content"]) and any(
+ isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"}
+ for b in m["content"]
+ ):
+ m["_thinking_signature_invalidated"] = True
+ m["content"] = kept if kept else [{"type": "text", "text": "(tool call removed)"}]
+
+ # Pass 2: Rebuild the set of tool_use IDs that survived pass 1, then
+ # strip tool_result blocks that no longer have any matching tool_use
+ # anywhere in the conversation.
+ surviving_tool_use_ids: set = set()
+ for m in result:
+ if m.get("role") == "assistant" and isinstance(m.get("content"), list):
+ for block in m["content"]:
+ if isinstance(block, dict) and block.get("type") == "tool_use":
+ surviving_tool_use_ids.add(block.get("id"))
+
+ for m in result:
+ if m.get("role") != "user" or not isinstance(m.get("content"), list):
+ continue
+ new_content = [
+ b
+ for b in m["content"]
+ if not (isinstance(b, dict) and b.get("type") == "tool_result")
+ or b.get("tool_use_id") in surviving_tool_use_ids
+ ]
+ if len(new_content) != len(m["content"]):
+ m["content"] = new_content if new_content else [{"type": "text", "text": "(tool result removed)"}]
+
+
+def _merge_consecutive_roles(result: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
+ """Merge consecutive same-role messages to enforce Anthropic alternation.
+
+ Returns a new list (caller must rebind ``result``).
+ """
+ fixed = []
+ for m in result:
+ if fixed and fixed[-1]["role"] == m["role"]:
+ if m["role"] == "user":
+ prev_content = fixed[-1]["content"]
+ curr_content = m["content"]
+ if isinstance(prev_content, str) and isinstance(curr_content, str):
+ fixed[-1]["content"] = prev_content + "\n" + curr_content
+ elif isinstance(prev_content, list) and isinstance(curr_content, list):
+ fixed[-1]["content"] = prev_content + curr_content
+ else:
+ if isinstance(prev_content, str):
+ prev_content = [{"type": "text", "text": prev_content}]
+ if isinstance(curr_content, str):
+ curr_content = [{"type": "text", "text": curr_content}]
+ fixed[-1]["content"] = prev_content + curr_content
+ else:
+ # Consecutive assistant messages — merge text content.
+ # Propagate the orphan-strip signature-invalidation flag onto the
+ # surviving (prev) dict so _manage_thinking_signatures still sees it.
+ if m.get("_thinking_signature_invalidated"):
+ fixed[-1]["_thinking_signature_invalidated"] = True
+ # Drop thinking blocks from the *second* message: their
+ # signature was computed against a different turn boundary
+ # and becomes invalid once merged.
+ if isinstance(m["content"], list):
+ m["content"] = [
+ b for b in m["content"]
+ if not (isinstance(b, dict) and b.get("type") in {"thinking", "redacted_thinking"})
+ ]
+ prev_blocks = fixed[-1]["content"]
+ curr_blocks = m["content"]
+ if isinstance(prev_blocks, list) and isinstance(curr_blocks, list):
+ fixed[-1]["content"] = prev_blocks + curr_blocks
+ elif isinstance(prev_blocks, str) and isinstance(curr_blocks, str):
+ fixed[-1]["content"] = prev_blocks + "\n" + curr_blocks
+ else:
+ if isinstance(prev_blocks, str):
+ prev_blocks = [{"type": "text", "text": prev_blocks}]
+ if isinstance(curr_blocks, str):
+ curr_blocks = [{"type": "text", "text": curr_blocks}]
+ fixed[-1]["content"] = prev_blocks + curr_blocks
+ else:
+ fixed.append(m)
+ return fixed
+
+
+def _manage_thinking_signatures(
+ result: List[Dict[str, Any]], base_url: str | None, model: str | None
+) -> None:
+ """Strip or preserve thinking blocks based on endpoint type.
+
+ Anthropic signs thinking blocks against the full turn content.
+ Any upstream mutation (context compression, session truncation, orphan
+ stripping, message merging) invalidates the signature, causing HTTP 400
+ "Invalid signature in thinking block".
+
+ Signatures are Anthropic-proprietary. Third-party endpoints (MiniMax,
+ Azure AI Foundry, AWS Bedrock, self-hosted proxies) cannot validate them
+ and will reject them outright. Kimi's /coding and DeepSeek's /anthropic
+ endpoints speak the Anthropic protocol upstream but require unsigned
+ thinking blocks (synthesised from ``reasoning_content``) to round-trip on
+ replayed assistant tool-call messages. See hermes-agent#13848 (Kimi) and
+ hermes-agent#16748 (DeepSeek).
+
+ Nous Portal's ``/v1/messages`` route is the exception among third-party
+ hosts: it proxies Claude to Anthropic/Vertex/Bedrock and validates the
+ same signed thinking blocks. Sticky ``session_id`` keeps a conversation
+ on one upstream instance so those signatures stay warm — stripping them
+ here would 400 the first tool-loop turn ("thinking must be passed back").
+ Portal therefore takes the native Anthropic replay path below.
+
+ Mutates ``result`` in place.
+ """
+ _THINKING_TYPES = frozenset(("thinking", "redacted_thinking"))
+ # Portal speaks Anthropic's thinking contract end-to-end; do not treat it
+ # as a signature-blind proxy even though the host is not anthropic.com.
+ _is_third_party = (
+ _is_third_party_anthropic_endpoint(base_url)
+ and not _is_nous_portal_endpoint(base_url)
+ )
+
+ last_assistant_idx = None
+ for i in range(len(result) - 1, -1, -1):
+ if result[i].get("role") == "assistant":
+ last_assistant_idx = i
+ break
+
+ for idx, m in enumerate(result):
+ if m.get("role") != "assistant" or not isinstance(m.get("content"), list):
+ continue
+
+ if _is_kimi_family_endpoint(base_url, model):
+ # Kimi does not enforce thinking signatures — replay as-is
+ # (shared cleanup below still strips cache markers + the internal flag).
+ pass
+ elif _is_deepseek_anthropic_endpoint(base_url):
+ # DeepSeek: strip signed, preserve unsigned.
+ new_content = []
+ for b in m["content"]:
+ if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES:
+ new_content.append(b)
+ continue
+ if b.get("signature") or b.get("data"):
+ # Signed (or redacted-with-data) — upstream can't validate, strip.
+ continue
+ new_content.append(b)
+ m["content"] = new_content or [{"type": "text", "text": "(empty)"}]
+ elif _is_third_party or idx != last_assistant_idx:
+ # Third-party: strip ALL thinking blocks (signatures are proprietary).
+ # Direct Anthropic: strip from non-latest assistant messages only.
+ stripped = [
+ b for b in m["content"]
+ if not (isinstance(b, dict) and b.get("type") in _THINKING_TYPES)
+ ]
+ m["content"] = stripped or [{"type": "text", "text": "(thinking elided)"}]
+ else:
+ # Latest assistant on direct Anthropic: keep signed, downgrade unsigned
+ # to text so the reasoning isn't lost.
+ #
+ # Exception: if orphan-stripping (or another structural mutation) removed
+ # a tool_use block from THIS turn, every thinking signature on it was
+ # computed against the original turn content and is now dead. Anthropic
+ # rejects the turn either way — replaying the signed block 400s with
+ # "thinking blocks in the latest assistant message cannot be modified",
+ # and a bare signed block with no following tool_use is also invalid.
+ # Demote ALL thinking blocks on this turn to text so the turn replays
+ # cleanly and the model can re-plan from the surviving tool results.
+ signature_dead = bool(m.get("_thinking_signature_invalidated"))
+ new_content = []
+ for b in m["content"]:
+ if not isinstance(b, dict) or b.get("type") not in _THINKING_TYPES:
+ new_content.append(b)
+ continue
+ if signature_dead:
+ thinking_text = b.get("thinking", "")
+ if thinking_text:
+ new_content.append({"type": "text", "text": thinking_text})
+ continue
+ if b.get("type") == "redacted_thinking":
+ # Redacted blocks use 'data' for the signature payload —
+ # drop the block when 'data' is missing (can't be validated).
+ if b.get("data"):
+ new_content.append(b)
+ elif b.get("signature"):
+ new_content.append(b)
+ else:
+ thinking_text = b.get("thinking", "")
+ if thinking_text:
+ new_content.append({"type": "text", "text": thinking_text})
+ m["content"] = new_content or [{"type": "text", "text": "(empty)"}]
+
+ # Strip cache_control from any remaining thinking/redacted_thinking
+ # blocks — cache markers interfere with signature validation.
+ for b in m["content"]:
+ if isinstance(b, dict) and b.get("type") in _THINKING_TYPES:
+ b.pop("cache_control", None)
+
+ # Drop the internal bookkeeping flag — it must never reach the API payload.
+ m.pop("_thinking_signature_invalidated", None)
+
+
+def _evict_old_screenshots(result: List[Dict[str, Any]]) -> None:
+ """Keep only the most recent ``_MAX_KEEP_IMAGES`` computer-use screenshots.
+
+ Base64 images cost ~1,465 tokens each and accumulate across tool calls.
+ Walk backward, keep the most recent N, replace older ones with a placeholder.
+
+ Mutates ``result`` in place.
+ """
+ _MAX_KEEP_IMAGES = 3
+ _image_count = 0
+ for msg in reversed(result):
+ content = msg.get("content")
+ if not isinstance(content, list):
+ continue
+ for block in content:
+ if not isinstance(block, dict) or block.get("type") != "tool_result":
+ continue
+ inner = block.get("content")
+ if not isinstance(inner, list):
+ continue
+ has_image = any(
+ isinstance(b, dict) and b.get("type") == "image"
+ for b in inner
+ )
+ if not has_image:
+ continue
+ _image_count += 1
+ if _image_count > _MAX_KEEP_IMAGES:
+ block["content"] = [
+ b if b.get("type") != "image"
+ else {"type": "text", "text": "[screenshot removed to save context]"}
+ for b in inner
+ ]
+
+
+def _ensure_leading_user_turn(result: List[Dict[str, Any]]) -> None:
+ """Anthropic requires messages[0] to have role=user.
+
+ After a second context compaction on the auto path the summary can be
+ emitted as role=assistant with nothing in front of it (the system prompt
+ lives outside messages[] or is extracted into the separate ``system``
+ param), so messages[0] ends up assistant and the Messages API rejects
+ the request with HTTP 400 — often masked by a misleading
+ "tool_use ids were found without tool_result blocks" error (#52160).
+
+ Mirror the Bedrock Converse adapter, which unconditionally prepends a
+ minimal user turn when the first message is not user
+ (convert_messages_to_converse).
+
+ The inserted text block must be non-whitespace: Anthropic separately
+ rejects any text content block whose text is empty or whitespace-only
+ ("text content blocks must contain non-whitespace text"), so a single
+ space here traded the "leading assistant turn" 400 for that one (#69512
+ class). Uses the same placeholder as every other synthesized filler
+ block in this module for consistency.
+ """
+ if result and result[0].get("role") != "user":
+ result.insert(
+ 0, {"role": "user", "content": [{"type": "text", "text": _EMPTY_TEXT_PLACEHOLDER}]}
+ )
+
+
+def _fix_blank_text_blocks_in_list(
+ blocks: List[Any],
+ *,
+ placeholder_text: str,
+ msg_index: int,
+ role: Any,
+ location: str,
+) -> List[Any]:
+ """Drop blank/whitespace-only text blocks from ``blocks``, in place logic.
+
+ Non-text blocks (tool_use, tool_result, image, document, thinking, …)
+ and the relative order of everything else are left untouched. A
+ cache_control marker riding on a dropped block is relocated onto the
+ last surviving text/tool_use block so a breakpoint is never silently
+ lost. If nothing survives, a single non-blank placeholder text block
+ takes the dropped blocks' place (carrying the relocated cache_control,
+ if any) so the message never has empty content.
+
+ Returns a new list; does not mutate ``blocks``.
+ """
+ kept: List[Any] = []
+ relocated_cache_control = None
+ for block_index, blk in enumerate(blocks):
+ if (
+ isinstance(blk, dict)
+ and blk.get("type") == "text"
+ and not (isinstance(blk.get("text"), str) and blk["text"].strip())
+ ):
+ if isinstance(blk.get("cache_control"), dict):
+ relocated_cache_control = blk["cache_control"]
+ logger.warning(
+ "Pre-call sanitizer: dropped blank text content block "
+ "(message_index=%d role=%s location=%s block_index=%d "
+ "block_type=text)",
+ msg_index,
+ role,
+ location,
+ block_index,
+ )
+ continue
+ kept.append(blk)
+ if not kept:
+ placeholder: Dict[str, Any] = {"type": "text", "text": placeholder_text}
+ if relocated_cache_control is not None:
+ placeholder["cache_control"] = relocated_cache_control
+ kept.append(placeholder)
+ elif relocated_cache_control is not None:
+ _apply_assistant_cache_control_to_last_cacheable_block(kept, relocated_cache_control)
+ return kept
+
+
+def _scrub_blank_text_blocks(result: List[Dict[str, Any]]) -> None:
+ """Final provider-boundary guard against blank Anthropic text blocks.
+
+ Anthropic rejects any text content block whose ``text`` is empty or
+ whitespace-only with HTTP 400 ("text content blocks must contain
+ non-whitespace text"). ``_convert_assistant_message``,
+ ``_convert_user_message`` and ``_ensure_leading_user_turn`` already
+ avoid emitting these for the paths that build them, but this pass runs
+ last — after every other transform in ``convert_messages_to_anthropic``
+ — so a blank block from any current or future producer (including one
+ nested inside a ``tool_result``'s own content list) never reaches the
+ wire. Diagnostics are structural only: message index, role, content
+ location, block index/type. Never logs message text, tool arguments,
+ tokens, or credentials. Mutates ``result`` in place.
+ """
+ for msg_index, msg in enumerate(result):
+ if not isinstance(msg, dict):
+ continue
+ role = msg.get("role")
+ content = msg.get("content")
+ if not isinstance(content, list) or not content:
+ continue
+ placeholder_text = _EMPTY_TEXT_PLACEHOLDER if role == "assistant" else "(empty message)"
+ new_content = _fix_blank_text_blocks_in_list(
+ content,
+ placeholder_text=placeholder_text,
+ msg_index=msg_index,
+ role=role,
+ location="content",
+ )
+ for blk in new_content:
+ if not isinstance(blk, dict) or blk.get("type") != "tool_result":
+ continue
+ inner = blk.get("content")
+ if isinstance(inner, list) and inner:
+ blk["content"] = _fix_blank_text_blocks_in_list(
+ inner,
+ placeholder_text="(no output)",
+ msg_index=msg_index,
+ role=role,
+ location="tool_result",
+ )
+ msg["content"] = new_content
+
+
+def convert_messages_to_anthropic(
+ messages: List[Dict],
+ base_url: str | None = None,
+ model: str | None = None,
+) -> Tuple[Optional[Any], List[Dict]]:
+ """Convert OpenAI-format messages to Anthropic format.
+
+ Returns (system_prompt, anthropic_messages).
+ System messages are extracted since Anthropic takes them as a separate param.
+ system_prompt is a string or list of content blocks (when cache_control present).
+
+ When *base_url* is provided and points to a third-party Anthropic-compatible
+ endpoint, all thinking block signatures are stripped. Signatures are
+ Anthropic-proprietary — third-party endpoints cannot validate them and will
+ reject them with HTTP 400 "Invalid signature in thinking block".
+
+ When *model* is provided and matches the Kimi / Moonshot family (or
+ *base_url* is a Kimi / Moonshot host), unsigned thinking blocks
+ synthesised from ``reasoning_content`` are preserved on replayed
+ assistant tool-call messages — Kimi requires the field to exist, even
+ if empty.
+ """
+ system = None
+ result: List[Dict[str, Any]] = []
+
+ for m in messages:
+ role = m.get("role", "user")
+ content = m.get("content", "")
+
+ if role == "system":
+ if isinstance(content, list):
+ # Preserve cache_control markers on content blocks
+ has_cache = any(
+ p.get("cache_control") for p in content if isinstance(p, dict)
+ )
+ if has_cache:
+ # Copy blocks before coercing so the caller's message
+ # dicts are never mutated, then replace blank/whitespace
+ # text with the shared non-whitespace placeholder —
+ # Anthropic rejects a blank system text block with the
+ # same HTTP 400 as message blocks ("text content blocks
+ # must contain non-whitespace text"), and a blank block
+ # carrying a cache_control breakpoint cannot simply be
+ # dropped (#70909).
+ system = []
+ for p in content:
+ if not isinstance(p, dict):
+ continue
+ if (
+ p.get("type") == "text"
+ and isinstance(p.get("text"), str)
+ and not p["text"].strip()
+ ):
+ p = dict(p)
+ p["text"] = _EMPTY_TEXT_PLACEHOLDER
+ system.append(p)
+ else:
+ system = "\n".join(
+ p["text"] for p in content if p.get("type") == "text"
+ )
+ else:
+ system = content
+ continue
+
+ if role == "assistant":
+ result.append(_convert_assistant_message(m))
+ continue
+
+ if role == "tool":
+ _convert_tool_message_to_result(result, m)
+ continue
+
+ # Regular user message
+ result.append(_convert_user_message(content))
+
+ _strip_orphaned_tool_blocks(result)
+ result = _merge_consecutive_roles(result)
+ _ensure_leading_user_turn(result)
+ _manage_thinking_signatures(result, base_url, model)
+ _evict_old_screenshots(result)
+ _scrub_blank_text_blocks(result)
+
+ return system, result
+
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index 88887d7aaaf..7ddb60f7c67 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -5135,7 +5135,7 @@ def _refresh_provider_credentials(provider: str) -> bool:
_evict_cached_clients(normalized)
return True
if normalized == "anthropic":
- from agent.anthropic_adapter import read_claude_code_credentials, _refresh_oauth_token, resolve_anthropic_token
+ from agent.anthropic_credentials import read_claude_code_credentials, _refresh_oauth_token, resolve_anthropic_token
creds = read_claude_code_credentials()
token = _refresh_oauth_token(creds) if isinstance(creds, dict) and creds.get("refreshToken") else None
diff --git a/agent/credential_pool.py b/agent/credential_pool.py
index 40bd24adac2..4353086d970 100644
--- a/agent/credential_pool.py
+++ b/agent/credential_pool.py
@@ -940,7 +940,7 @@ class CredentialPool:
if self.provider != "anthropic" or entry.source != "claude_code":
return entry
try:
- from agent.anthropic_adapter import read_claude_code_credentials
+ from agent.anthropic_credentials import read_claude_code_credentials
creds = read_claude_code_credentials()
if not creds:
return entry
@@ -1440,7 +1440,7 @@ class CredentialPool:
# the lock, the in-lock re-sync below picks up the rotated token the
# winner persisted and skips the POST.
# Anthropic's OAuth refresh tokens are single-use too (see
- # agent/anthropic_adapter.py::_refresh_oauth_token), so the same
+ # agent/anthropic_credentials.py::_refresh_oauth_token), so the same
# cross-process serialization Codex/xAI get is required here.
# Previously "anthropic" was excluded from this tuple: two Hermes
# processes racing to refresh the same stale token would both POST,
@@ -1504,7 +1504,7 @@ class CredentialPool:
refresh a ``claude_code``-sourced Anthropic entry, not just callers
sharing one profile's ``auth.json``.
"""
- from agent.anthropic_adapter import claude_code_credentials_path
+ from agent.anthropic_credentials import claude_code_credentials_path
return _auth_store_lock(
timeout_seconds=self._single_use_refresh_lock_timeout(),
@@ -1581,7 +1581,7 @@ class CredentialPool:
) -> Optional[PooledCredential]:
try:
if self.provider == "anthropic":
- from agent.anthropic_adapter import refresh_anthropic_oauth_pure
+ from agent.anthropic_credentials import refresh_anthropic_oauth_pure
refreshed = refresh_anthropic_oauth_pure(
entry.refresh_token,
@@ -1598,7 +1598,7 @@ class CredentialPool:
# see the latest tokens.
if entry.source == "claude_code":
try:
- from agent.anthropic_adapter import _write_claude_code_credentials
+ from agent.anthropic_credentials import _write_claude_code_credentials
_write_claude_code_credentials(
refreshed["access_token"],
refreshed["refresh_token"],
@@ -1623,7 +1623,7 @@ class CredentialPool:
# a second authority for the same refresh-token family.
elif entry.source == "hermes_pkce":
try:
- from agent.anthropic_adapter import _write_hermes_oauth_credentials
+ from agent.anthropic_credentials import _write_hermes_oauth_credentials
_write_hermes_oauth_credentials(
refreshed["access_token"],
refreshed["refresh_token"],
@@ -1691,7 +1691,7 @@ class CredentialPool:
if synced.refresh_token != entry.refresh_token:
logger.debug("Retrying refresh with synced token from credentials file")
try:
- from agent.anthropic_adapter import refresh_anthropic_oauth_pure
+ from agent.anthropic_credentials import refresh_anthropic_oauth_pure
refreshed = refresh_anthropic_oauth_pure(
synced.refresh_token,
use_json=synced.source.endswith("hermes_pkce"),
@@ -1702,7 +1702,7 @@ class CredentialPool:
# unbacked, and the next load_pool() re-seeded the
# consumed pair straight over it.
try:
- from agent.anthropic_adapter import _write_claude_code_credentials
+ from agent.anthropic_credentials import _write_claude_code_credentials
_write_claude_code_credentials(
refreshed["access_token"],
refreshed["refresh_token"],
@@ -2851,7 +2851,7 @@ def _seed_from_singletons(provider: str, entries: List[PooledCredential]) -> Tup
changed = True
return changed, active_sources
- from agent.anthropic_adapter import read_claude_code_credentials, read_hermes_oauth_credentials
+ from agent.anthropic_credentials import read_claude_code_credentials, read_hermes_oauth_credentials
for source_name, creds in (
("hermes_pkce", read_hermes_oauth_credentials()),
diff --git a/tests/agent/test_anthropic_adapter.py b/tests/agent/test_anthropic_adapter.py
index e4cba60440b..9619925194b 100644
--- a/tests/agent/test_anthropic_adapter.py
+++ b/tests/agent/test_anthropic_adapter.py
@@ -155,7 +155,7 @@ class TestReadClaudeCodeCredentials:
@pytest.fixture(autouse=True)
def no_keychain(self, monkeypatch):
monkeypatch.setattr(
- "agent.anthropic_adapter._read_claude_code_credentials_from_keychain",
+ "agent.anthropic_credentials._read_claude_code_credentials_from_keychain",
lambda: None,
)
@@ -253,7 +253,7 @@ class TestResolveAnthropicToken:
monkeypatch.delenv("ANTHROPIC_TOKEN", raising=False)
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
self._assert_not_called,
)
@@ -283,7 +283,7 @@ class TestResolveAnthropicToken:
# Isolate source #5 (credential_pool): ensure source #4 (Claude Code
# creds, incl. the macOS keychain read which Path.home does not cover)
# returns nothing, mirroring a Hermes-PKCE-only setup.
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
pool_entry = SimpleNamespace(
auth_type="oauth",
@@ -302,7 +302,7 @@ class TestResolveAnthropicToken:
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
self._assert_not_called,
)
monkeypatch.setattr(
@@ -321,7 +321,7 @@ class TestResolveAnthropicToken:
monkeypatch.delenv("ANTHROPIC_TOKEN", raising=False)
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
broken_entry = SimpleNamespace(auth_type="oauth", access_token=None)
pool = SimpleNamespace(
@@ -341,7 +341,7 @@ class TestResolveAnthropicToken:
monkeypatch.delenv("ANTHROPIC_TOKEN", raising=False)
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
api_key_entry = SimpleNamespace(auth_type="api_key", access_token="sk-pool-apikey")
pool = SimpleNamespace(
@@ -361,7 +361,7 @@ class TestResolveAnthropicToken:
monkeypatch.delenv("ANTHROPIC_TOKEN", raising=False)
monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
captured = {}
pool_entry = SimpleNamespace(auth_type="oauth", access_token="pool-oauth-token")
@@ -402,7 +402,7 @@ class TestRefreshOauthToken:
# so the adopt-already-refreshed branch can't short-circuit with a real
# credential on a dev/CI machine that happens to have Claude Code creds.
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
creds = {"accessToken": "expired", "refreshToken": "", "expiresAt": 0}
assert _refresh_oauth_token(creds) is None
@@ -410,7 +410,7 @@ class TestRefreshOauthToken:
def test_successful_refresh(self, tmp_path, monkeypatch):
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
creds = {
@@ -446,7 +446,7 @@ class TestRefreshOauthToken:
def test_failed_refresh_returns_none(self, tmp_path, monkeypatch):
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
creds = {
"accessToken": "old",
@@ -519,7 +519,7 @@ class TestResolveWithRefresh:
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
# Mock refresh to succeed
- with patch("agent.anthropic_adapter._refresh_oauth_token", return_value="refreshed-token"):
+ with patch("agent.anthropic_credentials._refresh_oauth_token", return_value="refreshed-token"):
result = resolve_anthropic_token()
assert result == "refreshed-token"
@@ -540,7 +540,7 @@ class TestResolveWithRefresh:
}))
monkeypatch.setattr("agent.anthropic_adapter.Path.home", lambda: tmp_path)
- with patch("agent.anthropic_adapter._refresh_oauth_token", return_value="refreshed-token"):
+ with patch("agent.anthropic_credentials._refresh_oauth_token", return_value="refreshed-token"):
result = resolve_anthropic_token()
assert result == "refreshed-token"
diff --git a/tests/agent/test_anthropic_credential_persist_failure.py b/tests/agent/test_anthropic_credential_persist_failure.py
index 532ab75c6fa..c3b7c27dbfe 100644
--- a/tests/agent/test_anthropic_credential_persist_failure.py
+++ b/tests/agent/test_anthropic_credential_persist_failure.py
@@ -33,8 +33,8 @@ import time
import pytest
-from agent import anthropic_adapter as AA
-from agent.anthropic_adapter import CredentialPersistError
+from agent import anthropic_credentials as AA
+from agent.anthropic_credentials import CredentialPersistError
from agent.credential_pool import (
AUTH_TYPE_OAUTH,
CREDENTIAL_PERSIST_FAILED_REASON,
diff --git a/tests/agent/test_anthropic_keychain.py b/tests/agent/test_anthropic_keychain.py
index da9967f4209..7f1174967c2 100644
--- a/tests/agent/test_anthropic_keychain.py
+++ b/tests/agent/test_anthropic_keychain.py
@@ -211,7 +211,7 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
the network refresh entirely.
"""
monkeypatch.setattr(
- "agent.anthropic_adapter.claude_code_credentials_path",
+ "agent.anthropic_credentials.claude_code_credentials_path",
lambda: tmp_path / ".claude" / ".credentials.json",
)
fresh = {
@@ -220,7 +220,7 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
"expiresAt": self._FRESH,
}
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: fresh,
)
@@ -228,7 +228,7 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
raise AssertionError("refresh_anthropic_oauth_pure must not be called")
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
_should_not_be_called,
)
@@ -242,12 +242,12 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
ourselves using the freshest available refresh token.
"""
monkeypatch.setattr(
- "agent.anthropic_adapter.claude_code_credentials_path",
+ "agent.anthropic_credentials.claude_code_credentials_path",
lambda: tmp_path / ".claude" / ".credentials.json",
)
# Live read returns an expired credential carrying a refresh token.
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {"accessToken": "expired", "refreshToken": "live-refresh", "expiresAt": 1},
)
captured = {}
@@ -261,10 +261,10 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
}
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure", _fake_refresh
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure", _fake_refresh
)
monkeypatch.setattr(
- "agent.anthropic_adapter._write_claude_code_credentials",
+ "agent.anthropic_credentials._write_claude_code_credentials",
lambda *a, **k: None,
)
@@ -277,7 +277,7 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
"""Direct resolver refreshes must not spend one Claude token twice."""
shared_credentials_path = tmp_path / ".claude" / ".credentials.json"
monkeypatch.setattr(
- "agent.anthropic_adapter.claude_code_credentials_path",
+ "agent.anthropic_credentials.claude_code_credentials_path",
lambda: shared_credentials_path,
)
@@ -315,9 +315,9 @@ class TestRefreshOAuthTokenAdoptsFreshCredential:
"expires_at_ms": self._FRESH,
}
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", read_credentials)
- monkeypatch.setattr("agent.anthropic_adapter._write_claude_code_credentials", write_credentials)
- monkeypatch.setattr("agent.anthropic_adapter.refresh_anthropic_oauth_pure", refresh)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", read_credentials)
+ monkeypatch.setattr("agent.anthropic_credentials._write_claude_code_credentials", write_credentials)
+ monkeypatch.setattr("agent.anthropic_credentials.refresh_anthropic_oauth_pure", refresh)
results = {}
errors = {}
diff --git a/tests/agent/test_anthropic_oauth_stress.py b/tests/agent/test_anthropic_oauth_stress.py
index c800f535d49..b558eb4a6aa 100644
--- a/tests/agent/test_anthropic_oauth_stress.py
+++ b/tests/agent/test_anthropic_oauth_stress.py
@@ -45,7 +45,7 @@ def _process_claude_code_refresh_worker(
"""Refresh one shared Claude Code credential from an independent process."""
os.environ["HERMES_HOME"] = profile_home
- from agent import anthropic_adapter as anthropic_mod
+ from agent import anthropic_credentials as anthropic_mod
from agent import credential_pool as credential_pool_mod
from hermes_cli import auth as auth_mod
@@ -190,11 +190,11 @@ def test_high_concurrency_anthropic_refresh_no_lost_updates_no_deadlock(
"""
server = _SingleUseTokenServer(delay_seconds=0.02)
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: server.refresh(refresh_token, use_json=use_json),
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
shared_stale_entry = _entry(
diff --git a/tests/agent/test_anthropic_token_scope_isolation.py b/tests/agent/test_anthropic_token_scope_isolation.py
index 88275397202..6f3a48c1ab4 100644
--- a/tests/agent/test_anthropic_token_scope_isolation.py
+++ b/tests/agent/test_anthropic_token_scope_isolation.py
@@ -50,8 +50,8 @@ def _pin_file_and_pool_sources():
This isolates the three os.getenv() call sites (sources 1, 2, 5) so each
test exercises exactly the env-var reading behaviour under scope control.
"""
- with patch("agent.anthropic_adapter.read_claude_code_credentials", return_value=None), \
- patch("agent.anthropic_adapter._resolve_anthropic_pool_token", return_value=None):
+ with patch("agent.anthropic_credentials.read_claude_code_credentials", return_value=None), \
+ patch("agent.anthropic_credentials._resolve_anthropic_pool_token", return_value=None):
yield
diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.py
index a345cac3ee4..2c4232a81f6 100644
--- a/tests/agent/test_auxiliary_client.py
+++ b/tests/agent/test_auxiliary_client.py
@@ -2584,17 +2584,20 @@ class TestAuxiliaryAuthRefreshRetry:
with (
patch("agent.auxiliary_client._client_cache", {cache_key: (stale_client, "claude-haiku-4-5-20251001", None)}),
- patch("agent.anthropic_adapter.read_claude_code_credentials", return_value={
+ # Anthropic credential sourcing lives in agent/anthropic_credentials.py;
+ # patch it at that definition site so both the direct call here and
+ # the re-read inside ``_refresh_oauth_token`` see the same stub.
+ patch("agent.anthropic_credentials.read_claude_code_credentials", return_value={
"accessToken": "expired-token",
"refreshToken": "refresh-token",
"expiresAt": 0,
}),
- patch("agent.anthropic_adapter.refresh_anthropic_oauth_pure", return_value={
+ patch("agent.anthropic_credentials.refresh_anthropic_oauth_pure", return_value={
"access_token": "fresh-token",
"refresh_token": "refresh-token-2",
"expires_at_ms": 9999999999999,
}) as mock_refresh_oauth,
- patch("agent.anthropic_adapter._write_claude_code_credentials") as mock_write,
+ patch("agent.anthropic_credentials._write_claude_code_credentials") as mock_write,
):
from agent.auxiliary_client import _refresh_provider_credentials
diff --git a/tests/agent/test_credential_pool.py b/tests/agent/test_credential_pool.py
index 72d2f04d013..edc370a7767 100644
--- a/tests/agent/test_credential_pool.py
+++ b/tests/agent/test_credential_pool.py
@@ -152,7 +152,7 @@ def test_stale_credential_id_prefers_api_key_hint(tmp_path, monkeypatch):
healthy key must not inherit the primary's 429.
"""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
_write_auth_store(
tmp_path,
{
@@ -211,7 +211,7 @@ def test_unmatched_api_key_hint_rotates_without_benching_innocent_key(tmp_path,
# Keep the dev machine's live ~/.claude credentials from seeding a
# claude_code singleton entry into this pool (same isolation as the
# other anthropic pool tests in this file).
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
_write_auth_store(
tmp_path,
{
@@ -1113,8 +1113,8 @@ def test_load_pool_api_key_path_skips_oauth_autodiscovery(tmp_path, monkeypatch)
"expiresAt": int(time.time() * 1000) + 3_600_000,
}
- monkeypatch.setattr("agent.anthropic_adapter.read_hermes_oauth_credentials", _fake_pkce)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", _fake_cc)
+ monkeypatch.setattr("agent.anthropic_credentials.read_hermes_oauth_credentials", _fake_pkce)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", _fake_cc)
from agent.credential_pool import load_pool
@@ -1167,8 +1167,8 @@ def test_load_pool_api_key_path_prunes_stale_oauth_entries(tmp_path, monkeypatch
},
)
monkeypatch.setattr("hermes_cli.auth.is_provider_explicitly_configured", lambda pid: True)
- monkeypatch.setattr("agent.anthropic_adapter.read_hermes_oauth_credentials", lambda: None)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_hermes_oauth_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
from agent.credential_pool import load_pool
@@ -1196,11 +1196,11 @@ def test_load_pool_oauth_path_still_autodiscovers(tmp_path, monkeypatch):
monkeypatch.setattr("hermes_cli.auth.is_provider_explicitly_configured", lambda pid: True)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_hermes_oauth_credentials",
+ "agent.anthropic_credentials.read_hermes_oauth_credentials",
lambda: None,
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {
"accessToken": "sk-ant-oat01-autodiscovered-cc",
"refreshToken": "cc-refresh",
@@ -1366,11 +1366,11 @@ def test_load_pool_does_not_seed_claude_code_when_anthropic_not_configured(tmp_p
# Claude Code credentials exist on disk
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {"accessToken": "sk-ant...oken", "refreshToken": "rt", "expiresAt": 9999999999999},
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_hermes_oauth_credentials",
+ "agent.anthropic_credentials.read_hermes_oauth_credentials",
lambda: None,
)
# User configured kimi-coding, NOT anthropic
@@ -1780,8 +1780,8 @@ def test_persist_preserves_concurrent_disk_only_entry(tmp_path, monkeypatch):
# Block external-credential autodiscovery: a real ~/.claude/.credentials.json
# on a dev machine would seed an extra claude_code entry and break the
# exact-id assertions below (passes on CI where no such file exists).
- monkeypatch.setattr("agent.anthropic_adapter.read_hermes_oauth_credentials", lambda: None)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_hermes_oauth_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
_write_auth_store(
tmp_path,
{
@@ -1856,11 +1856,11 @@ def _make_anthropic_claude_code_pool(tmp_path, monkeypatch, *, access_token, ref
_write_auth_store(tmp_path, {"version": 1, "credential_pool": {}})
monkeypatch.setattr("hermes_cli.auth.is_provider_explicitly_configured", lambda pid: pid == "anthropic")
monkeypatch.setattr(
- "agent.anthropic_adapter.read_hermes_oauth_credentials",
+ "agent.anthropic_credentials.read_hermes_oauth_credentials",
lambda: None,
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {"accessToken": access_token, "refreshToken": refresh_token, "expiresAt": expires_at_ms},
)
from agent.credential_pool import load_pool
@@ -1884,7 +1884,7 @@ def test_sync_anthropic_entry_tokens_unchanged_no_op(tmp_path, monkeypatch):
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {"accessToken": "same-access", "refreshToken": "same-refresh", "expiresAt": 9_999_999_999_000},
)
@@ -1922,7 +1922,7 @@ def test_sync_anthropic_entry_clears_all_error_fields(tmp_path, monkeypatch):
pool._replace_entry(entry, exhausted)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: {"accessToken": "fresh-access", "refreshToken": "fresh-refresh", "expiresAt": 9_999_999_999_000},
)
diff --git a/tests/agent/test_credential_pool_anthropic_refresh_race.py b/tests/agent/test_credential_pool_anthropic_refresh_race.py
index 1fd7de043b2..813e4cdec96 100644
--- a/tests/agent/test_credential_pool_anthropic_refresh_race.py
+++ b/tests/agent/test_credential_pool_anthropic_refresh_race.py
@@ -9,7 +9,7 @@ otherwise "two processes can both adopt the same on-disk token, both POST
it, and the loser gets ``refresh_token_reused``".
Anthropic's OAuth refresh tokens have the identical single-use property --
-``agent.anthropic_adapter._refresh_oauth_token`` says so explicitly:
+``agent.anthropic_credentials._refresh_oauth_token`` says so explicitly:
"Claude Code's OAuth refresh tokens are single-use: a successful refresh
rotates the pair and invalidates the old refresh token." Before the PR, ``"anthropic"`` was absent from the
``("openai-codex", "xai-oauth")`` tuple that gets the cross-process flock,
@@ -136,7 +136,7 @@ def test_anthropic_refresh_is_protected_by_cross_process_lock(monkeypatch):
monkeypatch.setattr("agent.credential_pool._auth_store_lock", _RecordingLock)
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: {
"access_token": "sk-ant-oat-new",
"refresh_token": "sk-ant-ort-new",
@@ -144,7 +144,7 @@ def test_anthropic_refresh_is_protected_by_cross_process_lock(monkeypatch):
},
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
pool = CredentialPool(
@@ -171,12 +171,12 @@ def test_concurrent_hermes_pkce_refresh_loses_credential_despite_valid_token_on_
"""
server = _SingleUseTokenServer()
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: server.refresh(refresh_token, use_json=use_json),
)
# No Claude Code credential file in play for this scenario.
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials", lambda: None
+ "agent.anthropic_credentials.read_claude_code_credentials", lambda: None
)
shared_stale_entry = _entry(
@@ -253,7 +253,7 @@ def test_concurrent_claude_code_refresh_recovers_via_credentials_file(monkeypatc
"""
server = _SingleUseTokenServer()
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: server.refresh(refresh_token, use_json=use_json),
)
@@ -273,11 +273,11 @@ def test_concurrent_claude_code_refresh_recovers_via_credentials_file(monkeypatc
)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
_fake_read_claude_code_credentials,
)
monkeypatch.setattr(
- "agent.anthropic_adapter._write_claude_code_credentials",
+ "agent.anthropic_credentials._write_claude_code_credentials",
_fake_write_claude_code_credentials,
)
diff --git a/tests/agent/test_credential_pool_oat_authtype.py b/tests/agent/test_credential_pool_oat_authtype.py
index db05e73bf07..468e2b1e960 100644
--- a/tests/agent/test_credential_pool_oat_authtype.py
+++ b/tests/agent/test_credential_pool_oat_authtype.py
@@ -34,7 +34,7 @@ def test_load_heals_legacy_row_and_exposes_it_to_resolver(tmp_path, monkeypatch)
for key in ("ANTHROPIC_API_KEY", "ANTHROPIC_TOKEN", "CLAUDE_CODE_OAUTH_TOKEN"):
monkeypatch.delenv(key, raising=False)
monkeypatch.setattr(
- "agent.anthropic_adapter.read_claude_code_credentials",
+ "agent.anthropic_credentials.read_claude_code_credentials",
lambda: None,
)
token = "sk-ant-oat-legacy-manual"
diff --git a/tests/agent/test_credential_pool_oauth_writethrough.py b/tests/agent/test_credential_pool_oauth_writethrough.py
index cd844aa27b0..1ec55264032 100644
--- a/tests/agent/test_credential_pool_oauth_writethrough.py
+++ b/tests/agent/test_credential_pool_oauth_writethrough.py
@@ -339,14 +339,14 @@ def test_hermes_pkce_refresh_writes_back_to_singleton(tmp_path, monkeypatch):
_write_store(hermes_home / "auth.json", {"version": 1, "providers": {}})
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: {
"access_token": "sk-ant-oat-rt1",
"refresh_token": "rt1",
"expires_at_ms": int(time.time() * 1000) + 3_600_000,
},
)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
entry = PooledCredential(
provider="anthropic",
@@ -388,9 +388,9 @@ def test_manual_hermes_pkce_refresh_does_not_create_duplicate_singleton(
hermes_home.mkdir(parents=True, exist_ok=True)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
monkeypatch.setattr("hermes_cli.auth.is_provider_explicitly_configured", lambda pid: True)
- monkeypatch.setattr("agent.anthropic_adapter.read_claude_code_credentials", lambda: None)
+ monkeypatch.setattr("agent.anthropic_credentials.read_claude_code_credentials", lambda: None)
monkeypatch.setattr(
- "agent.anthropic_adapter.refresh_anthropic_oauth_pure",
+ "agent.anthropic_credentials.refresh_anthropic_oauth_pure",
lambda refresh_token, use_json=False: {
"access_token": "manual-at-1",
"refresh_token": "manual-rt-1",