The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compaction): clarify runtime capability state
Stephen Chin5247a6f07f47
diff --git a/agent/agent_init.py b/agent/agent_init.pyindex e482570b8d0..f92d781f7a9 100644--- a/agent/agent_init.py+++ b/agent/agent_init.py@@ -2831,9 +2831,10 @@ def init_agent(agent.codex_responses_native_compaction = codex_responses_native_compactionagent.codex_responses_compact_threshold = codex_responses_compact_thresholdfrom agent.native_compaction import resolve_native_compaction_capabilities- agent.capabilities = resolve_native_compaction_capabilities(+ agent.runtime_capabilities = resolve_native_compaction_capabilities(model=agent.model,base_url=agent.base_url,+ provider=agent.provider,is_codex_backend=(agent.provider or "").strip().lower() == "openai-codex",)agent.max_compression_attempts = compression_max_attemptsdiff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex 2b3587a9410..6a2e0f2b8d7 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -1748,8 +1748,17 @@ def restore_primary_runtime(agent) -> bool:if hasattr(agent, "_transport_cache"):agent._transport_cache.clear()agent.api_key = rt["api_key"]- if "capabilities" in rt:- agent.capabilities = dict(rt.get("capabilities") or {})+ if "runtime_capabilities" in rt:+ raw_capabilities = rt["runtime_capabilities"]+ if not isinstance(raw_capabilities, dict):+ logger.warning("Ignoring malformed runtime capabilities snapshot")+ else:+ agent.runtime_capabilities = dict(raw_capabilities)+ elif "capabilities" in rt:+ # Read snapshots written by the initial capability propagation patch.+ raw_capabilities = rt["capabilities"]+ if isinstance(raw_capabilities, dict):+ agent.runtime_capabilities = dict(raw_capabilities)agent._reasoning_echo_flag = rt.get("reasoning_echo_flag", False)agent.request_overrides = dict(rt.get("request_overrides") or {})agent._client_kwargs = dict(rt["client_kwargs"])@@ -2932,6 +2941,11 @@ def switch_model(if not api_mode:api_mode = determine_api_mode(new_provider, base_url, model=new_model)+ normalized_new_provider = (new_provider or "").strip().lower()+ if not base_url and normalized_new_provider == "openai":+ # An omitted URL means the provider's canonical direct endpoint.+ base_url = "https://api.openai.com/v1"+# Same-provider switches may omit base_url intentionally (for example, a# direct caller refreshing credentials). Resolve capabilities from the# endpoint that the normalization below will retain, not from the empty@@ -2948,6 +2962,7 @@ def switch_model(else resolve_native_compaction_capabilities(model=new_model,base_url=effective_base_url,+ provider=new_provider,is_codex_backend=(new_provider or '').strip().lower() == 'openai-codex',))@@ -2995,7 +3010,7 @@ def switch_model("_is_anthropic_oauth","_config_context_length","_reasoning_echo_flag",- "capabilities",+ "runtime_capabilities",)}# _client_kwargs is a dict — snapshot a shallow copy so mutating the@@ -3306,7 +3321,7 @@ def switch_model(# Publish the destination capability map only after every runtime setup# above has succeeded. Failed switches must leave the old map intact.- agent.capabilities = destination_capabilities+ agent.runtime_capabilities = destination_capabilities# ── Reset the cross-turn stale-call circuit breaker (#58962) ──# The breaker's error text tells the user to "switch models ... then@@ -3335,7 +3350,7 @@ def switch_model(# recovery or fallback restore would resurrect the PRE-switch# overrides via the stale init-time snapshot (#75091 seam)."request_overrides": dict(getattr(agent, "request_overrides", {}) or {}),- "capabilities": dict(getattr(agent, "capabilities", {}) or {}),+ "runtime_capabilities": dict(getattr(agent, "runtime_capabilities", {}) or {}),"compressor_model": getattr(_cc, "model", agent.model) if _cc else agent.model,"compressor_base_url": getattr(_cc, "base_url", agent.base_url) if _cc else agent.base_url,"compressor_api_key": getattr(_cc, "api_key", "") if _cc else "",diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 009f6a56904..8b8abc2023a 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -2933,9 +2933,10 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> bool# single stream attempt._reset_stale_streak(agent)from agent.native_compaction import resolve_native_compaction_capabilities- agent.capabilities = resolve_native_compaction_capabilities(+ agent.runtime_capabilities = resolve_native_compaction_capabilities(model=agent.model,base_url=agent.base_url,+ provider=fb_provider,is_codex_backend=fb_provider == "openai-codex",)return Truediff --git a/agent/native_compaction.py b/agent/native_compaction.pyindex 8d098adf4f6..e010943b888 100644--- a/agent/native_compaction.py+++ b/agent/native_compaction.py@@ -72,6 +72,7 @@ def resolve_native_compaction_capabilities(*,model: Optional[str],base_url: Optional[str],+ provider: Optional[str] = None,is_codex_backend: bool = False,) -> Dict[str, bool]:"""Resolve the native-compaction capability for a runtime destination.@@ -79,9 +80,11 @@ def resolve_native_compaction_capabilities(The result is deliberately explicit: a resolved ``False`` is differentfrom an unresolved capability and must survive model switches unchanged."""- eligible = is_native_compaction_model(model) and is_direct_openai_route(- base_url,- is_codex_backend=is_codex_backend,+ normalized_provider = (provider or "").strip().lower()+ direct_default = normalized_provider == "openai" and not base_url+ eligible = is_native_compaction_model(model) and (+ direct_default+ or is_direct_openai_route(base_url, is_codex_backend=is_codex_backend))return {"native_compaction": eligible}@@ -178,7 +181,7 @@ def native_compaction_context_management((``agent.codex_responses_native_compaction = False``, set by theconversation loop's rejection recovery) takes effect on the next call."""- capabilities = getattr(agent, "capabilities", None)+ capabilities = getattr(agent, "runtime_capabilities", None)if isinstance(capabilities, dict):if not bool(capabilities.get("native_compaction", False)):return Nonediff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.pyindex e5db94f47ab..f2e72cbf0e1 100644--- a/hermes_cli/model_switch.py+++ b/hermes_cli/model_switch.py@@ -2178,6 +2178,7 @@ def switch_model(runtime_capabilities = resolve_native_compaction_capabilities(model=new_model,base_url=base_url,+ provider=target_provider,is_codex_backend=target_provider.strip().lower() == "openai-codex",)diff --git a/tests/run_agent/test_fallback_reasoning_override.py b/tests/run_agent/test_fallback_reasoning_override.pyindex 349508942d5..40526e9a8c3 100644--- a/tests/run_agent/test_fallback_reasoning_override.py+++ b/tests/run_agent/test_fallback_reasoning_override.py@@ -75,7 +75,7 @@ class TestFallbackReasoningOverride:"client_kwargs": {},"use_prompt_caching": False,"use_native_cache_layout": False,- "capabilities": {"native_compaction": True},+ "runtime_capabilities": {"native_compaction": True},"reasoning_config": {"enabled": True, "effort": "medium"},"compressor_model": "gemini-flash","compressor_base_url": "",@@ -96,7 +96,7 @@ class TestFallbackReasoningOverride:agent.model = "claude-opus-4.5"agent.provider = "anthropic"agent.reasoning_config = {"enabled": True, "effort": "xhigh"}- agent.capabilities = {"native_compaction": False}+ agent.runtime_capabilities = {"native_compaction": False}agent.context_compressor = MagicMock()agent.base_url = ""agent._anthropic_prompt_cache_policy = MagicMock(return_value=(False, False))@@ -107,7 +107,7 @@ class TestFallbackReasoningOverride:assert result is True# reasoning_config should be restored to primary's value (medium)assert agent.reasoning_config == {"enabled": True, "effort": "medium"}- assert agent.capabilities == {"native_compaction": True}+ assert agent.runtime_capabilities == {"native_compaction": True}def test_fallback_global_fallback_with_yaml_false(self):"""Fallback global fallback must not coerce YAML boolean False.diff --git a/tests/run_agent/test_native_compaction_switch_capabilities.py b/tests/run_agent/test_native_compaction_switch_capabilities.pyindex dde00c4beab..21fe6a6d2a8 100644--- a/tests/run_agent/test_native_compaction_switch_capabilities.py+++ b/tests/run_agent/test_native_compaction_switch_capabilities.py@@ -36,6 +36,16 @@ def test_untrusted_destination_capabilities_are_explicitly_denied():assert capabilities == {"native_compaction": False}+def test_default_openai_destination_is_enabled_without_explicit_base_url():+ capabilities = resolve_native_compaction_capabilities(+ model="gpt-5.6",+ base_url="",+ provider="openai",+ )++ assert capabilities == {"native_compaction": True}++def test_explicit_false_capability_denies_native_payload():agent = _agent({"native_compaction": False})diff --git a/tests/run_agent/test_switch_model_context.py b/tests/run_agent/test_switch_model_context.pyindex 66427515cdd..828b1e1c78c 100644--- a/tests/run_agent/test_switch_model_context.py+++ b/tests/run_agent/test_switch_model_context.py@@ -139,14 +139,29 @@ def test_switch_model_omitted_base_url_preserves_direct_openai_capability():agent.provider = "openai"agent.model = "gpt-5.6"agent.base_url = "https://api.openai.com/v1"- agent.capabilities = {"native_compaction": True}+ agent.runtime_capabilities = {"native_compaction": True}agent._create_openai_client = lambda *_args, **_kwargs: MagicMock()with patch("agent.model_metadata.get_model_context_length", return_value=128_000):agent.switch_model("gpt-5.6", "openai", api_key="sk-new")assert agent.base_url == "https://api.openai.com/v1"- assert agent.capabilities == {"native_compaction": True}+ assert agent.runtime_capabilities == {"native_compaction": True}+++def test_cross_provider_switch_to_default_openai_preserves_native_capability():+ agent = _make_agent_with_compressor(config_context_length=None)+ agent.provider = "openrouter"+ agent.model = "gpt-5.5"+ agent.base_url = "https://openrouter.ai/api/v1"+ agent.runtime_capabilities = {"native_compaction": False}+ agent._create_openai_client = lambda *_args, **_kwargs: MagicMock()++ with patch("agent.model_metadata.get_model_context_length", return_value=128_000):+ agent.switch_model("gpt-5.6", "openai", api_key="sk-new")++ assert agent.base_url == "https://api.openai.com/v1"+ assert agent.runtime_capabilities == {"native_compaction": True}def test_direct_start_model_override_does_not_inherit_profile_context_length():@@ -304,7 +319,7 @@ def test_lmstudio_switch_uses_destination_context_and_verified_runtime(monkeypatdef test_later_lmstudio_failure_restores_runtime_capabilities(monkeypatch):agent = _make_agent_with_compressor(config_context_length=32_768)- agent.capabilities = {"native_compaction": True}+ agent.runtime_capabilities = {"native_compaction": True}original_client = agent.clientmonkeypatch.setattr(@@ -326,4 +341,4 @@ def test_later_lmstudio_failure_restores_runtime_capabilities(monkeypatch):assert agent.model == "primary-model"assert agent.provider == "openrouter"assert agent.client is original_client- assert agent.capabilities == {"native_compaction": True}+ assert agent.runtime_capabilities == {"native_compaction": True}diff --git a/tests/run_agent/test_switch_model_rollback.py b/tests/run_agent/test_switch_model_rollback.pyindex 8f8f7686a98..d5a1abf2c06 100644--- a/tests/run_agent/test_switch_model_rollback.py+++ b/tests/run_agent/test_switch_model_rollback.py@@ -46,7 +46,7 @@ def _make_agent_openrouter():agent._fallback_chain = []agent._fallback_model = Noneagent._config_context_length = None- agent.capabilities = {"native_compaction": False}+ agent.runtime_capabilities = {"native_compaction": False}return agent@@ -74,7 +74,7 @@ def _make_agent_anthropic():agent._fallback_chain = []agent._fallback_model = Noneagent._config_context_length = None- agent.capabilities = {"native_compaction": False}+ agent.runtime_capabilities = {"native_compaction": False}return agent@@ -110,7 +110,7 @@ def test_openai_client_rebuild_failure_rolls_back_to_original_state():assert agent.api_key == "or-key-original"assert agent.client is original_clientassert agent._client_kwargs == original_kwargs- assert agent.capabilities == {"native_compaction": False}+ assert agent.runtime_capabilities == {"native_compaction": False}def test_anthropic_client_rebuild_failure_rolls_back_to_original_state():