Skip to content

The agent that grows with you

refactor(cli): unify duplicated model-switch staging and last-assistant-text blocks

Teknium79aeb22886c9
diff --git a/hermes_cli/cli_loops_mixin.py b/hermes_cli/cli_loops_mixin.py
index 91fc398eec0..6b974adeec9 100644
--- a/hermes_cli/cli_loops_mixin.py
+++ b/hermes_cli/cli_loops_mixin.py
@@ -542,6 +542,25 @@ class CLILoopsMixin:
except Exception:
pass
+ def _last_assistant_response_text(self) -> str:
+ """Text of the most recent assistant message ("" when none); multimodal parts are flattened."""
+ try:
+ hist = self.conversation_history or []
+ for msg in reversed(hist):
+ if msg.get("role") == "assistant":
+ content = msg.get("content", "")
+ if isinstance(content, list):
+ parts = [
+ p.get("text", "")
+ for p in content
+ if isinstance(p, dict) and p.get("type") in {"text", "output_text"}
+ ]
+ return "\n".join(t for t in parts if t)
+ return str(content or "")
+ except Exception:
+ pass
+ return ""
+
def _maybe_complete_loop_tick_after_turn(self) -> None:
"""Post-turn hook: evaluate a finished /loop wakeup turn.
@@ -571,24 +590,7 @@ class CLILoopsMixin:
)
return
- last_response = ""
- try:
- hist = self.conversation_history or []
- for msg in reversed(hist):
- if msg.get("role") == "assistant":
- content = msg.get("content", "")
- if isinstance(content, list):
- parts = [
- p.get("text", "")
- for p in content
- if isinstance(p, dict) and p.get("type") in {"text", "output_text"}
- ]
- last_response = "\n".join(t for t in parts if t)
- else:
- last_response = str(content or "")
- break
- except Exception:
- last_response = ""
+ last_response = self._last_assistant_response_text()
decision = mgr.complete_tick(last_response)
msg = decision.get("message") or ""
@@ -674,25 +676,7 @@ class CLILoopsMixin:
return
# Extract the agent's final response for this turn.
- last_response = ""
- try:
- hist = self.conversation_history or []
- for msg in reversed(hist):
- if msg.get("role") == "assistant":
- content = msg.get("content", "")
- if isinstance(content, list):
- # Multimodal content — flatten text parts.
- parts = [
- p.get("text", "")
- for p in content
- if isinstance(p, dict) and p.get("type") in {"text", "output_text"}
- ]
- last_response = "\n".join(t for t in parts if t)
- else:
- last_response = str(content or "")
- break
- except Exception:
- last_response = ""
+ last_response = self._last_assistant_response_text()
# Skip judging on empty/whitespace-only responses. These are almost
# always transient failures (API error, empty stream) where the
diff --git a/hermes_cli/cli_model_switch_mixin.py b/hermes_cli/cli_model_switch_mixin.py
index 10d8eed26c3..b47b8a649f3 100644
--- a/hermes_cli/cli_model_switch_mixin.py
+++ b/hermes_cli/cli_model_switch_mixin.py
@@ -506,41 +506,17 @@ class CLIModelSwitchMixin:
except Exception:
save_config_value("model.context_length", None)
- def _apply_model_switch_result(
- self, result, persist_global: bool, custom_providers=None
- ) -> None:
- from cli import HermesCLI, _cprint, logger, save_config_value
- if not result.success:
- _cprint(f" ✗ {result.error_message}")
- return
-
- if self.agent is not None:
- try:
- from hermes_cli.context_switch_guard import merge_preflight_compression_warning
-
- # Prefer the fresh inventory list (same source as switch_model /
- # TUI); fall back to the agent-init snapshot.
- _cp = (
- custom_providers
- if custom_providers is not None
- else getattr(self.agent, "_custom_providers", None)
- )
- merge_preflight_compression_warning(
- result,
- agent=self.agent,
- messages=list(self.conversation_history or []),
- custom_providers=_cp,
- config_context_length=getattr(self.agent, "_config_context_length", None),
- )
- except Exception as exc:
- logger.debug("preflight-compression switch warning failed: %s", exc)
-
- old_model = self.model
- # Snapshot the CLI-level credential/runtime fields BEFORE mutating them
- # so a failed in-place agent swap can roll the whole CLI back to the old
- # working model. Otherwise the broken credentials staged below leak into
- # the next turn's resolution even though the agent itself rolled back
- # (#50163).
+ def _stage_and_swap_model(self, result, old_model) -> bool:
+ """Stage ``result`` onto the CLI fields, then swap the live agent in place.
+
+ CLI-level fields are snapshotted first (mirrors the gateway) so a failed
+ in-place agent swap rolls the whole CLI back to the old working model —
+ otherwise the broken credentials staged here leak into the next turn's
+ resolution even though the agent itself rolled back (#50163). Returns
+ False after printing the failure (caller aborts the rest of the commit:
+ note + success print), so a failed switch is a no-op, not a dead session.
+ """
+ from cli import _cprint
_cli_snapshot = {
"model": self.model,
"provider": self.provider,
@@ -577,17 +553,47 @@ class CLIModelSwitchMixin:
capabilities=getattr(result, "runtime_capabilities", None),
)
except Exception as exc:
- # The agent rolled itself back to the old working model/client.
- # Roll the CLI's own staged fields back too and abort the rest
- # of the commit (note + success print) so a failed switch is a
- # no-op rather than a dead session (#50163).
for _k, _v in _cli_snapshot.items():
setattr(self, _k, _v)
_cprint(
f" ⚠ Model switch to {result.new_model} failed ({exc}); "
f"staying on {old_model}."
)
- return
+ return False
+ return True
+
+ def _apply_model_switch_result(
+ self, result, persist_global: bool, custom_providers=None
+ ) -> None:
+ from cli import HermesCLI, _cprint, logger, save_config_value
+ if not result.success:
+ _cprint(f" ✗ {result.error_message}")
+ return
+
+ if self.agent is not None:
+ try:
+ from hermes_cli.context_switch_guard import merge_preflight_compression_warning
+
+ # Prefer the fresh inventory list (same source as switch_model /
+ # TUI); fall back to the agent-init snapshot.
+ _cp = (
+ custom_providers
+ if custom_providers is not None
+ else getattr(self.agent, "_custom_providers", None)
+ )
+ merge_preflight_compression_warning(
+ result,
+ agent=self.agent,
+ messages=list(self.conversation_history or []),
+ custom_providers=_cp,
+ config_context_length=getattr(self.agent, "_config_context_length", None),
+ )
+ except Exception as exc:
+ logger.debug("preflight-compression switch warning failed: %s", exc)
+
+ old_model = self.model
+ if not self._stage_and_swap_model(result, old_model):
+ return
from hermes_cli.model_switch import format_model_for_display
_display_old = format_model_for_display(old_model)
@@ -932,54 +938,8 @@ class CLIModelSwitchMixin:
# overwrite the switch on the next turn (it re-resolves from this).
old_model = self.model
_one_turn_restore_snapshot = self._snapshot_model_runtime() if one_turn else None
- # Snapshot CLI-level fields before mutation so a failed in-place swap
- # rolls the whole CLI back to the old working model (#50163).
- _cli_snapshot = {
- "model": self.model,
- "provider": self.provider,
- "requested_provider": self.requested_provider,
- "_explicit_api_key": getattr(self, "_explicit_api_key", None),
- "_explicit_base_url": getattr(self, "_explicit_base_url", None),
- "api_key": self.api_key,
- "base_url": self.base_url,
- "api_mode": self.api_mode,
- }
- self.model = result.new_model
- self.provider = result.target_provider
- self.requested_provider = result.target_provider
- # Always overwrite explicit overrides so stale credentials from the
- # previous provider (e.g. Ollama api_key/base_url) don't leak into
- # the new provider's credential resolution on the next turn.
- self._explicit_api_key = result.api_key
- self._explicit_base_url = result.base_url
- if result.api_key:
- self.api_key = result.api_key
- if result.base_url:
- self.base_url = result.base_url
- if result.api_mode:
- self.api_mode = result.api_mode
-
- # Apply to running agent (in-place swap)
- if self.agent is not None:
- try:
- self.agent.switch_model(
- new_model=result.new_model,
- new_provider=result.target_provider,
- api_key=result.api_key,
- base_url=result.base_url,
- api_mode=result.api_mode,
- capabilities=getattr(result, "runtime_capabilities", None),
- )
- except Exception as exc:
- # Agent rolled itself back; roll the CLI back too and abort so a
- # failed switch is a no-op rather than a dead session (#50163).
- for _k, _v in _cli_snapshot.items():
- setattr(self, _k, _v)
- _cprint(
- f" ⚠ Model switch to {result.new_model} failed ({exc}); "
- f"staying on {old_model}."
- )
- return
+ if not self._stage_and_swap_model(result, old_model):
+ return
# Store a note to prepend to the next user message so the model
# knows a switch occurred (avoids injecting system messages mid-history
diff --git a/tests/hermes_cli/test_25106_global_switch_persists_base_url_api_mode.py b/tests/hermes_cli/test_25106_global_switch_persists_base_url_api_mode.py
index caba48e5fcb..a1a7b712cec 100644
--- a/tests/hermes_cli/test_25106_global_switch_persists_base_url_api_mode.py
+++ b/tests/hermes_cli/test_25106_global_switch_persists_base_url_api_mode.py
@@ -40,6 +40,11 @@ def _make_result(*, base_url="https://api.minimax.io/v1", api_mode="chat_complet
class _StubCLI:
"""Minimum attrs/methods `_handle_model_switch` reads or calls on self."""
+ def _stage_and_swap_model(self, result, old_model):
+ # Staging + in-place swap lives in a helper; run the real one on this stub.
+ import cli as _cli_mod
+ return _cli_mod.HermesCLI._stage_and_swap_model(self, result, old_model)
+
agent = None
model = "old-model"
diff --git a/tests/hermes_cli/test_apply_model_switch_result_context.py b/tests/hermes_cli/test_apply_model_switch_result_context.py
index d6742cc9917..dfea543bfd4 100644
--- a/tests/hermes_cli/test_apply_model_switch_result_context.py
+++ b/tests/hermes_cli/test_apply_model_switch_result_context.py
@@ -31,6 +31,11 @@ class _FakeModelInfo:
class _StubCLI:
"""Minimum attrs ``_apply_model_switch_result`` reads on ``self``."""
+ def _stage_and_swap_model(self, result, old_model):
+ # Staging + in-place swap lives in a helper; run the real one on this stub.
+ import cli as _cli_mod
+ return _cli_mod.HermesCLI._stage_and_swap_model(self, result, old_model)
+
agent = None
model = ""
provider = ""
diff --git a/tests/hermes_cli/test_cli_model_once.py b/tests/hermes_cli/test_cli_model_once.py
index 220040d833a..4e4be13c8a9 100644
--- a/tests/hermes_cli/test_cli_model_once.py
+++ b/tests/hermes_cli/test_cli_model_once.py
@@ -18,6 +18,11 @@ class _FakeAgent:
class _StubCLI:
+ def _stage_and_swap_model(self, result, old_model):
+ # Staging + in-place swap lives in a helper; run the real one on this stub.
+ import cli as _cli_mod
+ return _cli_mod.HermesCLI._stage_and_swap_model(self, result, old_model)
+
model = "old/model"
provider = "openrouter"
requested_provider = "openrouter"
diff --git a/tests/hermes_cli/test_model_switch_confirm_thread.py b/tests/hermes_cli/test_model_switch_confirm_thread.py
index 51a41442c5e..e5a8817f80a 100644
--- a/tests/hermes_cli/test_model_switch_confirm_thread.py
+++ b/tests/hermes_cli/test_model_switch_confirm_thread.py
@@ -33,6 +33,11 @@ class _FakeAgent:
class _StubCLI:
+ def _stage_and_swap_model(self, result, old_model):
+ # Staging + in-place swap lives in a helper; run the real one on this stub.
+ import cli as _cli_mod
+ return _cli_mod.HermesCLI._stage_and_swap_model(self, result, old_model)
+
model = "old/model"
provider = "openrouter"
requested_provider = "openrouter"