Skip to content

The agent that grows with you

refactor(cli): call GatewayAgent.switch_model directly for --once

Teknium81c3c1f96e63
diff --git a/hermes_cli/cli_model_switch_mixin.py b/hermes_cli/cli_model_switch_mixin.py
index 350175ac258..5f8fe6e305e 100644
--- a/hermes_cli/cli_model_switch_mixin.py
+++ b/hermes_cli/cli_model_switch_mixin.py
@@ -11,7 +11,6 @@ module-level functions taking ``cli`` and siblings are called as ``HermesCLI.<na
from __future__ import annotations
import copy
-import inspect
import logging
import sys
import threading
@@ -317,22 +316,6 @@ def _apply_reasoning_after_switch(cli, effort: str, *, persist_global: bool, one
_cprint(f" {t('cli.model.reasoning_effort_saved' if saved else 'cli.model.reasoning_effort', effort=effort)}")
-_ONE_TURN_SWITCH_KWARGS = ("once", "restore")
-
-
-def _call_agent_switch(agent, **fields):
- """``agent.switch_model`` with ``once`` / ``restore`` only when its signature names them.
-
- ``AIAgent.switch_model`` is a ``**kwargs`` forwarder to a function that rejects them; an
- in-process agent swaps in place, so it gets the plain switch."""
- try:
- named = inspect.signature(agent.switch_model).parameters
- except (TypeError, ValueError):
- named = {}
- return agent.switch_model(**{key: value for key, value in fields.items()
- if key not in _ONE_TURN_SWITCH_KWARGS or key in named})
-
-
def _commit_model_switch(
cli, result, *, persist_global: bool, one_turn: bool = False, picker: bool = False,
reasoning_effort: str = "") -> None:
@@ -739,8 +722,10 @@ class CLIModelSwitchMixin:
logger.debug("CLI one-turn model restore via primary runtime failed", exc_info=True)
if hasattr(agent, "switch_model"):
try:
- restored = _call_agent_switch(
- agent, new_model=snapshot.get("model", ""), new_provider=snapshot.get("provider", ""),
+ # The classic CLI only builds a GatewayAgent (``_init_agent``): its ``once`` / ``restore`` are
+ # named, so a rename fails loudly instead of turning the restore into a session mutate.
+ restored = agent.switch_model(
+ new_model=snapshot.get("model", ""), new_provider=snapshot.get("provider", ""),
api_key=snapshot.get("api_key", ""), base_url=snapshot.get("base_url", ""),
api_mode=snapshot.get("api_mode", ""),
capabilities=snapshot.get("capabilities"), once=True, restore=True)
@@ -829,7 +814,7 @@ class CLIModelSwitchMixin:
capabilities=getattr(result, "runtime_capabilities", None))
if getattr(self, "_model_switch_one_turn", False):
fields["once"] = True
- if _call_agent_switch(self.agent, **fields) is False:
+ if self.agent.switch_model(**fields) is False:
# Declined at the gateway owner's guard prompt: nothing was committed.
for _k, _v in _cli_snapshot.items():
setattr(self, _k, _v)
diff --git a/tests/hermes_cli/test_cli_gateway_model_scope.py b/tests/hermes_cli/test_cli_gateway_model_scope.py
index e6c1f9bb8b9..29a489d402f 100644
--- a/tests/hermes_cli/test_cli_gateway_model_scope.py
+++ b/tests/hermes_cli/test_cli_gateway_model_scope.py
@@ -236,43 +236,6 @@ def test_once_on_a_resumed_session_asks_before_it_switches(monkeypatch):
assert stub._pending_one_turn_model_restore is None
-def test_one_turn_kwargs_reach_only_a_switch_that_names_them():
- """``AIAgent.switch_model`` is a lazy ``**kwargs`` forwarder to a function without ``once`` /
- ``restore``: it gets the plain in-process switch. Its own errors still surface."""
- from hermes_cli.cli_model_switch_mixin import _call_agent_switch
- from run_agent import AIAgent
-
- seen = []
-
- def _in_process(agent, new_model, new_provider, api_key="", base_url="", api_mode="", capabilities=None):
- seen.append(new_model)
- if new_model == "broken":
- raise TypeError("provider client could not be built")
- return True
-
- class _Forwarder:
- switch_model = AIAgent.switch_model
-
- class _OneTurnAware:
- def switch_model(self, new_model, new_provider="", *, once=False, restore=False):
- seen.append((new_model, once, restore))
- return True
-
- import agent.agent_runtime_helpers as helpers
- original = helpers.switch_model
- helpers.switch_model = _in_process
- try:
- assert _call_agent_switch(_Forwarder(), new_model="next", new_provider="openrouter", once=True) is True
- assert _call_agent_switch(_Forwarder(), new_model="prev", new_provider="openrouter",
- once=True, restore=True) is True
- with pytest.raises(TypeError, match="could not be built"):
- _call_agent_switch(_Forwarder(), new_model="broken", new_provider="openrouter", once=True)
- finally:
- helpers.switch_model = original
- assert _call_agent_switch(_OneTurnAware(), new_model="next", once=True, restore=True) is True
- assert seen == ["next", "prev", "broken", ("next", True, True)]
-
-
def test_switching_to_the_route_the_session_runs_commits_nothing(monkeypatch):
conn = RecordingConnection({"model": "old/model", "provider": "openrouter"})
cli_mod, stub, _printed = _cli(monkeypatch, conn)