The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): honor prompt_caching.cache_ttl=off on stub policy paths
686f6c618a29703a1b12
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 072fe507f33..88423be486e 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -1857,6 +1857,27 @@ def _direct_native_anthropic_tool_cache_capability()+def prompt_caching_disabled_from_config() -> bool:+ """Return True when ``prompt_caching.cache_ttl`` is configured as off.++ Mirrors the disable detection in ``agent_init`` so stub-based policy+ paths (MoA slot decoration, auxiliary fallback replan) honor the same+ config contract without holding a live ``AIAgent`` (#76085 / #33555).+ """+ try:+ from hermes_cli.config import load_config_readonly++ pc_cfg = load_config_readonly().get("prompt_caching", {}) or {}+ ttl = pc_cfg.get("cache_ttl", "5m")+ except Exception:+ return False+ if ttl in {"5m", "1h"}:+ return False+ if ttl is False or ttl is None:+ return True+ return str(ttl).lower() in ("off", "false", "disabled", "no", "none")++def plan_cache_sections_for_destination(messages: list,tools: Optional[list],@@ -1865,6 +1886,7 @@ def plan_cache_sections_for_destination(base_url: str,api_mode: str,model: str,+ cache_disabled: Optional[bool] = None,) -> Tuple[list, list]:"""Plan request-local cache sections for one resolved destination.@@ -1877,6 +1899,11 @@ def plan_cache_sections_for_destination(Never mutates ``messages`` or ``tools`` — both return values arerequest-local copies.++ ``cache_disabled`` threads the operator's ``prompt_caching.cache_ttl``+ disable into the blank policy stub. When omitted, the live config is+ consulted so MoA/auxiliary paths cannot re-enable markers after the+ user turned caching off (#76085)."""from types import SimpleNamespace@@ -1886,7 +1913,15 @@ def plan_cache_sections_for_destination(strip_anthropic_tool_cache_control,)- stub = SimpleNamespace(provider="", base_url="", api_mode="", model="")+ if cache_disabled is None:+ cache_disabled = prompt_caching_disabled_from_config()+ stub = SimpleNamespace(+ provider="",+ base_url="",+ api_mode="",+ model="",+ _cache_disabled=bool(cache_disabled),+ )should_cache, native_layout = anthropic_prompt_cache_policy(stub,provider=provider,@@ -3895,6 +3930,8 @@ __all__ = ["restore_primary_runtime","extract_reasoning","dump_api_request_debug",+ "prompt_caching_disabled_from_config",+ "plan_cache_sections_for_destination","anthropic_prompt_cache_policy","create_openai_client","switch_model",diff --git a/agent/moa_loop.py b/agent/moa_loop.pyindex e149ea9f08c..d56121b864a 100644--- a/agent/moa_loop.py+++ b/agent/moa_loop.py@@ -383,6 +383,8 @@ def _merge_slot_extra_body(def _maybe_apply_moa_cache_control(messages: list[dict[str, Any]],runtime: dict[str, Any],+ *,+ cache_disabled: bool | None = None,) -> list[dict[str, Any]]:"""Decorate an advisor or aggregator request with cache_control when itsroute honors it.@@ -396,17 +398,33 @@ def _maybe_apply_moa_cache_control(Returns the messages unchanged on any resolution error or when thepolicy says the route doesn't honor markers.++ ``cache_disabled`` (or the live config when omitted) is stamped onto the+ policy stub so ``prompt_caching.cache_ttl: off`` is not bypassed by the+ blank-agent pattern (#76085)."""try:from types import SimpleNamespace- from agent.agent_runtime_helpers import anthropic_prompt_cache_policy+ from agent.agent_runtime_helpers import (+ anthropic_prompt_cache_policy,+ prompt_caching_disabled_from_config,+ )from agent.prompt_caching import apply_anthropic_cache_control+ if cache_disabled is None:+ cache_disabled = prompt_caching_disabled_from_config()+# The policy function reads agent.* only as fallbacks for kwargs we# don't pass; provide a stub so the slot is judged purely on its own- # resolved runtime.- stub = SimpleNamespace(provider="", base_url="", api_mode="", model="")+ # resolved runtime (plus the operator disable flag).+ stub = SimpleNamespace(+ provider="",+ base_url="",+ api_mode="",+ model="",+ _cache_disabled=bool(cache_disabled),+ )should_cache, native_layout = anthropic_prompt_cache_policy(stub,provider=runtime.get("provider") or "",@@ -1680,6 +1698,9 @@ class MoAChatCompletions:base_url=agg_runtime.get("base_url") or "",api_mode=agg_runtime.get("api_mode") or "",model=agg_runtime.get("model") or "",+ cache_disabled=bool(+ getattr(self._agent, "_cache_disabled", False)+ ),)if guidance:_attach_reference_guidance(agg_messages, str(guidance))diff --git a/tests/agent/test_cache_disabled_on_stubs.py b/tests/agent/test_cache_disabled_on_stubs.pynew file mode 100644index 00000000000..082edbaea38--- /dev/null+++ b/tests/agent/test_cache_disabled_on_stubs.py@@ -0,0 +1,172 @@+"""Regression for #76085: prompt_caching.cache_ttl off on stub policy paths.++Blank SimpleNamespace stubs used by MoA decoration and auxiliary/MoA+plan_cache_sections_for_destination never carried ``_cache_disabled``, so+``anthropic_prompt_cache_policy`` re-enabled cache_control markers even when+the operator set ``prompt_caching.cache_ttl: false``.+"""++from __future__ import annotations++from types import SimpleNamespace+from unittest.mock import patch++import pytest+++def _has_cache_control(obj) -> bool:+ if isinstance(obj, dict):+ if "cache_control" in obj:+ return True+ return any(_has_cache_control(v) for v in obj.values())+ if isinstance(obj, list):+ return any(_has_cache_control(v) for v in obj)+ return False+++class TestPromptCachingDisabledFromConfig:+ def test_off_values(self):+ from agent.agent_runtime_helpers import prompt_caching_disabled_from_config++ for ttl in (False, None, "off", "false", "disabled", "no", "none", "OFF"):+ with patch(+ "hermes_cli.config.load_config_readonly",+ return_value={"prompt_caching": {"cache_ttl": ttl}},+ ):+ assert prompt_caching_disabled_from_config() is True, ttl++ def test_enabled_values(self):+ from agent.agent_runtime_helpers import prompt_caching_disabled_from_config++ for ttl in ("5m", "1h"):+ with patch(+ "hermes_cli.config.load_config_readonly",+ return_value={"prompt_caching": {"cache_ttl": ttl}},+ ):+ assert prompt_caching_disabled_from_config() is False, ttl+++class TestPlanCacheSectionsHonorsDisable:+ def test_explicit_cache_disabled_strips_markers(self):+ from agent.agent_runtime_helpers import plan_cache_sections_for_destination++ messages = [+ {"role": "system", "content": "You are helpful."},+ {"role": "user", "content": "hello"},+ ]+ tools = [+ {+ "type": "function",+ "function": {+ "name": "web_search",+ "description": "search",+ "parameters": {"type": "object", "properties": {}},+ },+ }+ ]+ out_msgs, out_tools = plan_cache_sections_for_destination(+ messages,+ tools,+ provider="anthropic",+ base_url="https://api.anthropic.com",+ api_mode="anthropic_messages",+ model="claude-opus-4.8",+ cache_disabled=True,+ )+ assert not _has_cache_control(out_msgs)+ assert not _has_cache_control(out_tools)++ def test_config_off_without_explicit_flag(self):+ from agent.agent_runtime_helpers import plan_cache_sections_for_destination++ messages = [+ {"role": "system", "content": "You are helpful."},+ {"role": "user", "content": "hello"},+ ]+ with patch(+ "hermes_cli.config.load_config_readonly",+ return_value={"prompt_caching": {"cache_ttl": "off"}},+ ):+ out_msgs, out_tools = plan_cache_sections_for_destination(+ messages,+ None,+ provider="anthropic",+ base_url="https://api.anthropic.com",+ api_mode="anthropic_messages",+ model="claude-opus-4.8",+ )+ assert not _has_cache_control(out_msgs)+ assert out_tools is None or not _has_cache_control(out_tools)++ def test_enabled_still_adds_markers_on_native_anthropic(self):+ from agent.agent_runtime_helpers import plan_cache_sections_for_destination++ messages = [+ {"role": "system", "content": "You are helpful."},+ {"role": "user", "content": "hello"},+ {"role": "assistant", "content": "hi"},+ {"role": "user", "content": "again"},+ ]+ with patch(+ "hermes_cli.config.load_config_readonly",+ return_value={"prompt_caching": {"cache_ttl": "5m"}},+ ):+ out_msgs, _ = plan_cache_sections_for_destination(+ messages,+ None,+ provider="anthropic",+ base_url="https://api.anthropic.com",+ api_mode="anthropic_messages",+ model="claude-opus-4.8",+ cache_disabled=False,+ )+ assert _has_cache_control(out_msgs), (+ "With caching enabled, native Anthropic destinations must still "+ "receive cache_control breakpoints."+ )+++class TestMoASlotDecorationHonorsDisable:+ def test_maybe_apply_skips_markers_when_disabled(self):+ from agent.moa_loop import _maybe_apply_moa_cache_control++ messages = [+ {"role": "system", "content": "sys"},+ {"role": "user", "content": "q1"},+ {"role": "assistant", "content": "a1"},+ {"role": "user", "content": "q2"},+ ]+ runtime = {+ "provider": "anthropic",+ "model": "claude-opus-4.8",+ "base_url": "",+ "api_mode": "anthropic_messages",+ }+ out = _maybe_apply_moa_cache_control(+ messages, runtime, cache_disabled=True+ )+ assert not _has_cache_control(out)+ # Inputs must not be mutated.+ assert not _has_cache_control(messages)++ def test_maybe_apply_config_off(self):+ from agent.moa_loop import _maybe_apply_moa_cache_control++ messages = [+ {"role": "system", "content": "sys"},+ {"role": "user", "content": "q1"},+ {"role": "assistant", "content": "a1"},+ {"role": "user", "content": "q2"},+ ]+ runtime = {+ "provider": "anthropic",+ "model": "claude-opus-4.8",+ "base_url": "",+ "api_mode": "anthropic_messages",+ }+ with patch(+ "hermes_cli.config.load_config_readonly",+ return_value={"prompt_caching": {"cache_ttl": False}},+ ):+ out = _maybe_apply_moa_cache_control(messages, runtime)+ assert not _has_cache_control(out)