Skip to content

The agent that grows with you

refactor(agent): document meta api_mode fallback intent + review cleanups

Lavie9cf553ca3e32
diff --git a/agent/agent_init.py b/agent/agent_init.py
index c27806080a3..67fc38bf3f5 100644
--- a/agent/agent_init.py
+++ b/agent/agent_init.py
@@ -711,6 +711,12 @@ def init_agent(
# rewrites (e.g. api.anthropic.com → provider="anthropic", #63425)
# always run first. Covers api.meta.ai → codex_responses for prompt
# caching (0% on chat vs 93-99% on responses) and future mandates.
+ # Note: provider="meta" without an api.meta.ai base_url (or with a non-api.meta.ai
+ # base_url) intentionally falls through to chat_completions here. The wire
+ # protocol for Meta is URL-driven BY DESIGN, not provider-name-driven, because
+ # user config `providers.meta` may point at any OpenAI-compatible endpoint, and
+ # forcing `codex_responses` on the provider name alone would break custom endpoints
+ # named "meta" that do not host the Responses API.
try:
from hermes_cli.providers import host_mandated_api_mode as _host_mandated_api_mode
diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py
index 66a9d8de0c3..d68cf8da51e 100644
--- a/hermes_cli/runtime_provider.py
+++ b/hermes_cli/runtime_provider.py
@@ -190,6 +190,7 @@ def _resolve_plain_custom_api_mode(model_cfg: Dict[str, Any], base_url: str) ->
Responses path after upgrades or /reset.
"""
configured_mode = _parse_api_mode(model_cfg.get("api_mode"))
+ # Note: api.meta.ai is handled by _detect_api_mode_for_url (returns codex_responses), so the suppression guard below does not fire for Meta.
detected_mode = _detect_api_mode_for_url(base_url)
if configured_mode == "codex_responses" and detected_mode != "codex_responses":
diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.py
index 53cbf795caf..ad4966acaa3 100644
--- a/tests/agent/test_auxiliary_client.py
+++ b/tests/agent/test_auxiliary_client.py
@@ -2939,6 +2939,14 @@ class TestCodexAdapterPromptCacheKey:
])
assert captured["prompt_cache_retention"] == "24h"
+ def test_meta_endpoint_includes_prompt_cache_retention(self):
+ adapter, captured = self._build_adapter(base_url="https://api.meta.ai/v1", model="muse-spark-1.2")
+ adapter.create(messages=[
+ {"role": "system", "content": "SYS"},
+ {"role": "user", "content": "hi"},
+ ])
+ assert captured["prompt_cache_retention"] == "24h"
+
def test_prompt_cache_retention_skipped_for_codex_backend(self):
adapter, captured = self._build_adapter()
adapter.create(messages=[
diff --git a/tests/agent/test_meta_agent_init.py b/tests/agent/test_meta_agent_init.py
index 6eaf0cbffbe..60be0da0bf7 100644
--- a/tests/agent/test_meta_agent_init.py
+++ b/tests/agent/test_meta_agent_init.py
@@ -160,3 +160,41 @@ def test_agent_init_meta_url_without_provider_implies_codex_responses():
)
assert agent.api_mode == "codex_responses"
+
+def test_agent_init_meta_provider_without_meta_url_falls_through_to_chat_completions(monkeypatch):
+ """`provider="meta"` without an api.meta.ai base_url falls through to chat_completions.
+
+ The Meta wire protocol is URL-driven by design so custom OpenAI-compatible
+ endpoints configured under `providers.meta` are not broken by forcing Responses API.
+ """
+ from run_agent import AIAgent
+
+ monkeypatch.setenv("META_API_KEY", "sk-test-meta")
+
+ agent_empty = AIAgent(
+ provider="meta",
+ base_url="",
+ api_key="sk-test-meta",
+ model="muse-spark-1.2",
+ api_mode=None,
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ assert agent_empty.api_mode == "chat_completions"
+ assert agent_empty.provider == "meta"
+
+ agent_custom = AIAgent(
+ provider="meta",
+ base_url="https://custom.meta.endpoint.internal/v1",
+ api_key="sk-test-meta",
+ model="muse-spark-1.2",
+ api_mode=None,
+ quiet_mode=True,
+ skip_context_files=True,
+ skip_memory=True,
+ )
+ assert agent_custom.api_mode == "chat_completions"
+ assert agent_custom.provider == "meta"
+
+
diff --git a/tests/agent/transports/test_meta_codex_cache.py b/tests/agent/transports/test_meta_codex_cache.py
index 5dcb0ced8e4..6be2e7e8339 100644
--- a/tests/agent/transports/test_meta_codex_cache.py
+++ b/tests/agent/transports/test_meta_codex_cache.py
@@ -42,7 +42,7 @@ class TestMetaRetention:
assert _default_prompt_cache_retention_for_request("muse-spark-1.2", "https://API.META.AI/v1") == "24h"
assert _default_prompt_cache_retention_for_request("muse-spark-1.2", "https://api.meta.ai:443/v1") == "24h"
- def test_meta_retention_not_sent_when_overridden(self, transport):
+ def test_meta_retention_override_wins(self, transport):
kw = transport.build_kwargs(
model="muse-spark-1.2",
messages=[{"role": "user", "content": "Hi"}],
diff --git a/tests/hermes_cli/test_model_switch_openai_api_mode.py b/tests/hermes_cli/test_model_switch_openai_api_mode.py
index fe649b8ff92..8c7c1058d30 100644
--- a/tests/hermes_cli/test_model_switch_openai_api_mode.py
+++ b/tests/hermes_cli/test_model_switch_openai_api_mode.py
@@ -18,6 +18,7 @@ reasoning_effort into a live one that OpenAI 400s on the chat_completions path.
from unittest.mock import patch
from hermes_cli.model_switch import switch_model
+from hermes_cli.providers import host_mandated_api_mode
_MOCK_VALIDATION = {
"accepted": True,
@@ -91,8 +92,6 @@ def test_generic_endpoint_keeps_explicit_api_mode():
generic OpenAI-compatible relay returns None from host_mandated_api_mode,
so the switch path leaves the resolver's api_mode untouched.
"""
- from hermes_cli.providers import host_mandated_api_mode
-
assert host_mandated_api_mode("https://generic.example.com/v1") is None
# Lookalike / path-spoof hosts must also NOT be treated as mandated (#32243).
assert host_mandated_api_mode("https://api.openai.com.attacker.test/v1") is None
@@ -123,9 +122,7 @@ def test_stale_chat_overridden_on_meta_direct():
def test_generic_relay_not_clobbered_on_meta_switch():
"""Generic relay does not pick up Meta mandate."""
- assert __import__("hermes_cli.providers", fromlist=["host_mandated_api_mode"]).host_mandated_api_mode(
- "https://generic.example.com/v1"
- ) is None
+ assert host_mandated_api_mode("https://generic.example.com/v1") is None
result = _run_openai_switch(
raw_input="some-model",
current_provider="meta",