Skip to content

The agent that grows with you

fix(cli): send the alias a one-turn pick resolved through

Tekniumc0fc6d45dcf4
diff --git a/hermes_cli/cli_agent_setup_mixin.py b/hermes_cli/cli_agent_setup_mixin.py
index 2b3d42f37ff..2a7eab63472 100644
--- a/hermes_cli/cli_agent_setup_mixin.py
+++ b/hermes_cli/cli_agent_setup_mixin.py
@@ -688,6 +688,7 @@ class CLIAgentSetupMixin:
modal_withdraw_callback=None if single_query_mode else self._withdraw_gateway_modal,
one_turn=one_turn,
one_turn_reasoning=(pending_one_turn or {}).get("one_turn_reasoning") if one_turn else None,
+ one_turn_alias=(pending_one_turn or {}).get("one_turn_alias", "") if one_turn else "",
reasoning_callback=self._current_reasoning_callback(),
fallback_model=self._fallback_model, thinking_callback=self._on_thinking,
checkpoints_enabled=self.checkpoints_enabled,
diff --git a/hermes_cli/cli_model_switch_mixin.py b/hermes_cli/cli_model_switch_mixin.py
index 5f8fe6e305e..46ebed5ef6c 100644
--- a/hermes_cli/cli_model_switch_mixin.py
+++ b/hermes_cli/cli_model_switch_mixin.py
@@ -330,7 +330,9 @@ def _commit_model_switch(
or cli._snapshot_model_runtime()) if one_turn else None
if snapshot is not None:
# The level belongs to this --once pick only: a second pick without --reasoning drops it.
- snapshot = {key: value for key, value in snapshot.items() if key != "one_turn_reasoning"}
+ # So does the alias it resolved through.
+ snapshot = {key: value for key, value in snapshot.items()
+ if key not in ("one_turn_reasoning", "one_turn_alias")}
# ``--once`` must not session.mutate.
cli._model_switch_one_turn = bool(one_turn)
try:
@@ -347,6 +349,8 @@ def _commit_model_switch(
session_id=getattr(getattr(cli, "agent", None), "session_id", None) or getattr(cli, "session_id", None))
if not picker:
cli._pending_one_turn_model_restore = snapshot
+ if snapshot is not None and getattr(result, "resolved_via_alias", ""):
+ snapshot["one_turn_alias"] = result.resolved_via_alias # the agent chat rebuilds carries it
_print_switch_summary(cli, result, old_model, one_turn=one_turn, strict_context=not picker)
if reasoning_effort:
_apply_reasoning_after_switch(cli, reasoning_effort, persist_global=persist_global and not one_turn,
@@ -813,7 +817,8 @@ class CLIModelSwitchMixin:
api_key=result.api_key, base_url=result.base_url, api_mode=result.api_mode,
capabilities=getattr(result, "runtime_capabilities", None))
if getattr(self, "_model_switch_one_turn", False):
- fields["once"] = True
+ # The CLI and agent show the resolved model; the admission sends the alias.
+ fields.update(once=True, alias=getattr(result, "resolved_via_alias", "") or "")
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():
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index fefcc734e29..4d47f1574a1 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -78,7 +78,7 @@ class GatewayAgent:
tool_gen_callback=None, notice_callback=None, notice_clear_callback=None,
reaction_callback=None, interim_assistant_callback=None, connection=None,
push_reasoning=False, model_confirm_callback=None, modal_withdraw_callback=None,
- one_turn=False, one_turn_reasoning=None, **_ignored):
+ one_turn=False, one_turn_reasoning=None, one_turn_alias="", **_ignored):
conn = connection or getattr(session_db, "connection", None)
if conn is None:
raise GatewayClientError("Gateway session unavailable; no local fallback")
@@ -174,14 +174,15 @@ class GatewayAgent:
# below then overwrites ``model``): that preset rides the turn's submit as its model_override.
self._moa_preset = model if provider == "moa" else None
# ``/model --once``: chat retires the agent that switch_model touched and builds this one.
- # Capture the pick before ``_apply_info`` copies the session model over it.
- self._turn_route = self._route_off_session(model, provider) if one_turn else None
+ # Capture the pick, and the alias it resolved through, before ``_apply_info`` copies the session
+ # model over it.
+ self._turn_route = self._route_off_session(model, provider, one_turn_alias) if one_turn else None
self._taken_turn_route = None # the pick the admission being sent took (``_take_turn_route``)
# ``/model B --once --reasoning X``: X rides that admission's ``model_override``, never a session pin.
self._turn_reasoning = one_turn_reasoning if one_turn else None
self._apply_info(self._state().info)
if self._turn_route:
- self.model = self._turn_route["model"]
+ self.model = model # the resolved model, never the alias the admission sends
if self._turn_route.get("provider"):
self.provider = self._turn_route["provider"]
self.reasoning_callback = reasoning_callback
@@ -258,15 +259,19 @@ class GatewayAgent:
logger.debug("session.mutate reasoning failed: %s", exc)
return False
- def _route_off_session(self, model, provider=""):
- """``{model, provider}`` when the session runs another route, else None."""
+ def _route_off_session(self, model, provider="", alias=""):
+ """``{model, provider}`` the turn sends when the session runs another route, else None.
+
+ ``alias``, the ``model_aliases`` entry the pick resolved through, is sent instead of the bare
+ model: two aliases may name one model at different endpoints, so the owner must not re-resolve
+ it. An alias makes even the session's own model a one-turn override."""
state = self._state()
- if not model or not (state.info or {}).get("model") or state.runs_route(model, provider):
+ if not model or not (state.info or {}).get("model") or (state.runs_route(model, provider) and not alias):
return None
- return {"model": model, **({"provider": provider} if provider else {})}
+ return {"model": alias or model, **({"provider": provider} if provider else {})}
def switch_model(self, new_model, new_provider="", api_key="", base_url="", api_mode="", capabilities=None, *,
- once=False, restore=False):
+ once=False, restore=False, alias=""):
"""Session-scoped ``/model`` and /resume commit ``session.mutate model``.
``once=True`` does not write the session. The next ``chat`` builds a new agent; that
@@ -283,7 +288,8 @@ class GatewayAgent:
written; with no prompt the refusal raises. Any refusal raises: main's callers roll their
own fields back."""
if once or restore:
- self._turn_route = None if restore else self._route_off_session(new_model, new_provider)
+ # ``alias``: the ``model_aliases`` entry a ``--once`` pick resolved through (``_route_off_session``).
+ self._turn_route = None if restore else self._route_off_session(new_model, new_provider, alias)
if restore:
self._turn_reasoning = None
if new_model:
diff --git a/tests/hermes_cli/test_cli_gateway_model_scope.py b/tests/hermes_cli/test_cli_gateway_model_scope.py
index 29a489d402f..d56d90056bb 100644
--- a/tests/hermes_cli/test_cli_gateway_model_scope.py
+++ b/tests/hermes_cli/test_cli_gateway_model_scope.py
@@ -285,6 +285,42 @@ def test_once_reasoning_equal_to_the_inherited_level_commits_nothing(monkeypatch
assert _reasoning_mutates(conn) == []
[email protected]("session_model", ["shared-model", "other-model"])
+def test_once_sends_the_alias_it_resolved_through(monkeypatch, session_model):
+ """Review by @dokterdok: two ``model_aliases`` entries name one model on one provider, at different
+ endpoints. ``/model second --once`` sends ``second``, not the bare model the owner would re-resolve to
+ the first alias, and still sends it when the session already runs that model."""
+ from gateway.session_cli_turn import one_turn_runtime
+ from hermes_cli import model_switch
+ resolve = model_switch.switch_model
+ conn = RecordingConnection({"model": session_model, "provider": "custom"})
+ cli_mod, stub, printed = _chat_cli(monkeypatch, conn)
+ monkeypatch.setattr(model_switch, "switch_model", resolve) # the real resolver over these aliases
+ for name, port in (("first", 7002), ("second", 7003)):
+ monkeypatch.setitem(model_switch.DIRECT_ALIASES, name, model_switch.DirectAlias(
+ model="shared-model", provider="custom", base_url=f"http://127.0.0.1:{port}/v1", api_key=f"sk-{name}"))
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *_a, **_k: {
+ "accepted": True, "persist": True, "recognized": True, "message": ""})
+ monkeypatch.setattr("agent.models_dev.fetch_models_dev", dict)
+ session = {"provider": "custom", "api_key": "sk-session", "base_url": "http://127.0.0.1:7001/v1"}
+ stub.model, stub.provider, stub.requested_provider = session_model, "custom", "custom"
+ stub.base_url = stub._explicit_base_url = session["base_url"]
+ stub.api_key = stub._explicit_api_key = session["api_key"]
+ stub.agent.model, stub.agent.provider = session_model, "custom"
+ cli_mod.HermesCLI._handle_model_switch(stub, "/model second --once")
+ assert not [line for line in printed if "fail" in line.lower()], printed
+ assert stub.agent.model == "shared-model", "the agent shows the resolved model, not the alias"
+ stub.chat("run on second")
+ stub.chat("the turn after")
+ once, after = _submits(conn)
+ override = once.get("model_override")
+ assert override == {"model": "second", "provider": "custom"}, override
+ assert "model_override" not in after, after
+ _model, runtime = one_turn_runtime(override, session_model, {**session, "api_mode": "chat_completions"}, {})
+ assert (runtime["base_url"], runtime["api_key"]) == ("http://127.0.0.1:7003/v1", "sk-second")
+ assert not _model_routes(conn)[0]
+
+
class BusyOnceConnection(RecordingConnection):
"""The owner refuses the first ``prompt.submit`` with ``session_busy``: nothing was admitted."""