hermes-agent/tests/hermes_cli/test_determine_api_mode_hostname.py
Teknium cecf84daf7 fix: extend hostname-match provider detection across remaining call sites
Aslaaen's fix in the original PR covered _detect_api_mode_for_url and the
two openai/xai sites in run_agent.py. This finishes the sweep: the same
substring-match false-positive class (e.g. https://api.openai.com.evil/v1,
https://proxy/api.openai.com/v1, https://api.anthropic.com.example/v1)
existed in eight more call sites, and the hostname helper was duplicated
in two modules.

- utils: add shared base_url_hostname() (single source of truth).
- hermes_cli/runtime_provider, run_agent: drop local duplicates, import
  from utils. Reuse the cached AIAgent._base_url_hostname attribute
  everywhere it's already populated.
- agent/auxiliary_client: switch codex-wrap auto-detect, max_completion_tokens
  gate (auxiliary_max_tokens_param), and custom-endpoint max_tokens kwarg
  selection to hostname equality.
- run_agent: native-anthropic check in the Claude-style model branch
  and in the AIAgent init provider-auto-detect branch.
- agent/model_metadata: Anthropic /v1/models context-length lookup.
- hermes_cli/providers.determine_api_mode: anthropic / openai URL
  heuristics for custom/unknown providers (the /anthropic path-suffix
  convention for third-party gateways is preserved).
- tools/delegate_tool: anthropic detection for delegated subagent
  runtimes.
- hermes_cli/setup, hermes_cli/tools_config: setup-wizard vision-endpoint
  native-OpenAI detection (paired with deduping the repeated check into
  a single is_native_openai boolean per branch).

Tests:
- tests/test_base_url_hostname.py covers the helper directly
  (path-containing-host, host-suffix, trailing dot, port, case).
- tests/hermes_cli/test_determine_api_mode_hostname.py adds the same
  regression class for determine_api_mode, plus a test that the
  /anthropic third-party gateway convention still wins.

Also: add asslaenn5@gmail.com → Aslaaen to scripts/release.py AUTHOR_MAP.
2026-04-20 22:14:29 -07:00

43 lines
2.1 KiB
Python

"""Regression tests for ``determine_api_mode`` hostname handling.
Companion to tests/hermes_cli/test_detect_api_mode_for_url.py — the same
false-positive class (custom URLs containing ``api.openai.com`` /
``api.anthropic.com`` as a path segment or host suffix) must be rejected
by ``determine_api_mode`` as well, since it's the code path used by
custom/unknown providers in ``resolve_custom_provider``.
"""
from __future__ import annotations
from hermes_cli.providers import determine_api_mode
class TestOpenAIHostHardening:
def test_native_openai_url_is_codex_responses(self):
assert determine_api_mode("", "https://api.openai.com/v1") == "codex_responses"
def test_openai_host_suffix_is_not_codex(self):
assert determine_api_mode("", "https://api.openai.com.example/v1") == "chat_completions"
def test_openai_path_segment_is_not_codex(self):
assert determine_api_mode("", "https://proxy.example.test/api.openai.com/v1") == "chat_completions"
class TestAnthropicHostHardening:
def test_native_anthropic_url_is_anthropic_messages(self):
assert determine_api_mode("", "https://api.anthropic.com") == "anthropic_messages"
def test_anthropic_host_suffix_is_not_anthropic(self):
assert determine_api_mode("", "https://api.anthropic.com.example/v1") == "chat_completions"
def test_anthropic_path_segment_is_not_anthropic(self):
# A proxy whose path contains ``api.anthropic.com`` must not be misrouted.
# Note: the ``/anthropic`` convention for third-party gateways still wins
# via explicit path-suffix check — see test_anthropic_path_suffix_still_wins.
assert determine_api_mode("", "https://proxy.example.test/api.anthropic.com/v1") == "chat_completions"
def test_anthropic_path_suffix_still_wins(self):
# Third-party Anthropic-compatible gateways (MiniMax, Zhipu GLM, LiteLLM
# proxies) expose the Anthropic protocol under a ``/anthropic`` suffix.
# That convention must still resolve to anthropic_messages.
assert determine_api_mode("", "https://api.minimax.io/anthropic") == "anthropic_messages"