The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(compaction): resolve capability from effective switch URL
Stephen Chin903c36b6d4b0
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.pyindex fabfe751e71..2b3587a9410 100644--- a/agent/agent_runtime_helpers.py+++ b/agent/agent_runtime_helpers.py@@ -2922,6 +2922,9 @@ def switch_model(from hermes_cli.providers import determine_api_modefrom agent.native_compaction import resolve_native_compaction_capabilities+ old_model = agent.model+ old_provider = agent.provider+# ── Determine api_mode if not provided ──# Pass model so dual-wire providers (Nous Portal anthropic/* → Messages)# resolve correctly; without it determine_api_mode falls back to the@@ -2929,12 +2932,22 @@ def switch_model(if not api_mode:api_mode = determine_api_mode(new_provider, base_url, model=new_model)+ # 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+ # raw argument.+ effective_base_url = base_url+ if not effective_base_url and (old_provider or "").strip().lower() == (+ new_provider or ""+ ).strip().lower():+ effective_base_url = getattr(agent, "base_url", "")+destination_capabilities = (dict(capabilities)if isinstance(capabilities, dict)else resolve_native_compaction_capabilities(model=new_model,- base_url=base_url,+ base_url=effective_base_url,is_codex_backend=(new_provider or '').strip().lower() == 'openai-codex',))@@ -2954,9 +2967,6 @@ def switch_model():base_url = re.sub(r"/v1/?$", "", base_url)- old_model = agent.model- old_provider = agent.provider-# ── Snapshot all fields the swap+rebuild can mutate ──# If the rebuild raises (bad API key, network error, build_anthropic_client# failure, etc.) we restore these atomically so the agent isn't left with adiff --git a/tests/run_agent/test_switch_model_context.py b/tests/run_agent/test_switch_model_context.pyindex 8684baa7008..66427515cdd 100644--- a/tests/run_agent/test_switch_model_context.py+++ b/tests/run_agent/test_switch_model_context.py@@ -133,6 +133,22 @@ def test_switch_model_without_config_context_length():assert call_kwargs.get("config_context_length") is None+def test_switch_model_omitted_base_url_preserves_direct_openai_capability():+ """A same-provider switch resolves capabilities from the retained URL."""+ agent = _make_agent_with_compressor(config_context_length=None)+ agent.provider = "openai"+ agent.model = "gpt-5.6"+ agent.base_url = "https://api.openai.com/v1"+ agent.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}++def test_direct_start_model_override_does_not_inherit_profile_context_length():"""A CLI ``--model`` startup override must not inherit another model's window."""cfg = {