From d2af9900431b3dcabfb1d36760f6c8b1d21949ad Mon Sep 17 00:00:00 2001 From: Jack <4762467+apollo-orbit-dev@users.noreply.github.com> Date: Sat, 27 Jun 2026 12:07:50 -0500 Subject: [PATCH] fix(agent): carry request_overrides through in-place /model switch (TUI/CLI) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Third in the series. The gateway rebuild path (previous two commits) carries a custom provider's `request_overrides` (`extra_body`, e.g. `chat_template_kwargs`) into the agent, but the *in-place* live switch used by the TUI dashboard and the CLI — `agent.switch_model()` -> `agent_runtime_helpers.switch_model()` — swapped model/provider/base_url/api_key without ever updating `request_overrides`. So a `/model` switch to a thinking-enabled custom provider in the TUI/CLI kept the previous provider's `extra_body`. `switch_model()` now re-derives the switched-to provider's `request_overrides` (via `_get_named_custom_provider`) and applies it in place, preserving non-provider overrides (`service_tier`/`speed` from `/fast`). Logic factored into `_apply_switched_provider_request_overrides` for testability. Adds tests/agent/test_switch_model_request_overrides.py. --- agent/agent_runtime_helpers.py | 30 +++++++++++ .../test_switch_model_request_overrides.py | 52 +++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 tests/agent/test_switch_model_request_overrides.py diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py index dd30d99ed5..20699283e5 100644 --- a/agent/agent_runtime_helpers.py +++ b/agent/agent_runtime_helpers.py @@ -2848,6 +2848,29 @@ def create_openai_client(agent, client_kwargs: dict, *, reason: str, shared: boo return client +def _apply_switched_provider_request_overrides(agent, new_provider): + """Re-derive the switched-to provider's ``request_overrides`` onto a live agent. + + A ``custom_providers`` entry can carry an ``extra_body`` (e.g. + ``chat_template_kwargs`` to toggle a local model's thinking). The gateway + rebuild path carries this via ``request_overrides``; an *in-place* swap + (CLI / TUI ``/model``) must re-derive it for the new provider, otherwise the + previous provider's ``extra_body`` lingers. Non-provider overrides + (``service_tier`` / ``speed`` from ``/fast``) are preserved. + """ + from hermes_cli.runtime_provider import ( + _get_named_custom_provider, + _custom_provider_request_overrides, + ) + cp = _get_named_custom_provider(new_provider) + new_ro = _custom_provider_request_overrides(cp) if cp else None + overrides = dict(getattr(agent, "request_overrides", {}) or {}) + overrides.pop("extra_body", None) + if new_ro and new_ro.get("extra_body"): + overrides["extra_body"] = new_ro["extra_body"] + agent.request_overrides = overrides + + def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mode=''): """Switch the model/provider in-place for a live agent. @@ -3278,6 +3301,13 @@ def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mo agent._fallback_chain = fallback_chain agent._fallback_model = fallback_chain[0] if fallback_chain else None + # Apply the switched-to provider's request_overrides (custom_providers + # extra_body, e.g. chat_template_kwargs). See helper for rationale. + try: + _apply_switched_provider_request_overrides(agent, new_provider) + except Exception: + logger.debug("switch_model: request_overrides re-derivation failed", exc_info=True) + logger.info( "Model switched in-place: %s (%s) -> %s (%s)", old_model, old_provider, new_model, new_provider, diff --git a/tests/agent/test_switch_model_request_overrides.py b/tests/agent/test_switch_model_request_overrides.py new file mode 100644 index 0000000000..30543e896d --- /dev/null +++ b/tests/agent/test_switch_model_request_overrides.py @@ -0,0 +1,52 @@ +"""Regression tests for the in-place /model switch (CLI/TUI) carrying a custom +provider's request_overrides (extra_body) — _apply_switched_provider_request_overrides. + +Before the fix, agent_runtime_helpers.switch_model() swapped model/provider/ +base_url/api_key in place but never touched request_overrides, so a /model +switch to a thinking-enabled custom provider in the TUI/CLI kept the old +provider's extra_body. +""" + +import agent.agent_runtime_helpers as arh + + +class _Agent: + pass + + +def test_switch_applies_new_provider_extra_body(monkeypatch): + a = _Agent() + a.request_overrides = {"service_tier": "priority"} # pre-existing /fast override + monkeypatch.setattr( + "hermes_cli.runtime_provider._get_named_custom_provider", + lambda name: {"name": "main-think", + "extra_body": {"chat_template_kwargs": {"enable_thinking": True}}}, + ) + arh._apply_switched_provider_request_overrides(a, "custom:main-think") + assert a.request_overrides["extra_body"] == {"chat_template_kwargs": {"enable_thinking": True}} + assert a.request_overrides["service_tier"] == "priority" # preserved + + +def test_switch_to_noncustom_clears_stale_extra_body(monkeypatch): + a = _Agent() + a.request_overrides = { + "extra_body": {"chat_template_kwargs": {"enable_thinking": True}}, + "service_tier": "priority", + } + monkeypatch.setattr( + "hermes_cli.runtime_provider._get_named_custom_provider", lambda name: None + ) + arh._apply_switched_provider_request_overrides(a, "anthropic") + assert "extra_body" not in a.request_overrides # stale extra_body cleared + assert a.request_overrides["service_tier"] == "priority" # preserved + + +def test_switch_from_none_overrides(monkeypatch): + a = _Agent() + a.request_overrides = None + monkeypatch.setattr( + "hermes_cli.runtime_provider._get_named_custom_provider", + lambda name: {"name": "main", "extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}, + ) + arh._apply_switched_provider_request_overrides(a, "custom:main") + assert a.request_overrides == {"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}