Skip to content

The agent that grows with you

fix(telemetry): TUI /model before the first prompt blames the route it leaves

teknium1a01ffc467b69
diff --git a/tests/hermes_cli/test_shared_metrics_model_attribution.py b/tests/hermes_cli/test_shared_metrics_model_attribution.py
index f2cfa8e4f0f..adbbd90b3bc 100644
--- a/tests/hermes_cli/test_shared_metrics_model_attribution.py
+++ b/tests/hermes_cli/test_shared_metrics_model_attribution.py
@@ -110,6 +110,36 @@ def test_shipped_provider_and_public_model_are_reported_as_is(direct_runtime, tm
assert dims["model"] == PUBLIC, (name, dims)
[email protected](("configured", "expected"), [
+ (("custom", "acme-private-llama", "http://localhost:8080/v1"), ("custom", "custom")),
+ (("openrouter", PUBLIC, ""), ("openrouter", PUBLIC)),
+])
+def test_tui_switch_before_first_prompt_blames_the_configured_route(
+ direct_runtime, tmp_path, monkeypatch, configured, expected,
+):
+ """With no agent yet and ``--provider``, switch_away names the model the session was launched on
+ with ITS provider, never the target provider."""
+ provider, model, base_url = configured
+ home = tmp_path / "hermes-home"
+ home.mkdir(parents=True, exist_ok=True)
+ (home / "config.yaml").write_text(
+ f"model:\n provider: {provider}\n default: {model}\n" + (f" base_url: {base_url}\n" if base_url else ""))
+ import tui_gateway.server as server
+
+ monkeypatch.setattr(server, "_hermes_home", home) # captured at first import
+ result = SimpleNamespace(
+ success=True, new_model="gpt-5", target_provider="openai", base_url="https://api.openai.com/v1",
+ api_key="k", api_mode="chat_completions", warning_message="", error_message="")
+ monkeypatch.setattr("hermes_cli.model_switch.switch_model", lambda **kw: result)
+ server._apply_model_switch("", {"agent": None}, "gpt-5 --provider openai", confirm_expensive_model=True)
+ _flush()
+
+ assert _stored_values(tmp_path, "hermes.model_friction.count") == [
+ ({"model": expected[1], "provider": expected[0], "signal": "switch_away"}, 1)]
+ assert _stored_values(tmp_path, "hermes.model_switch.count") == [
+ ({"execution_surface": "tui", "from_provider": expected[0], "to_provider": "openai"}, 1)]
+
+
def _gateway_runner(multiplex_home=None):
from gateway.slash_commands_model import GatewayModelCommandsMixin
diff --git a/tui_gateway/model_switch.py b/tui_gateway/model_switch.py
index 6f08a8d2c4c..34e4576f4aa 100644
--- a/tui_gateway/model_switch.py
+++ b/tui_gateway/model_switch.py
@@ -216,6 +216,20 @@ def _current_model_runtime(agent, explicit_provider: str) -> tuple:
return provider, current_model, str(runtime.get("base_url", "") or ""), key
+def _switch_away_provider(agent, explicit_provider: str, current_provider: str) -> str | None:
+ """The provider of the model the user leaves. Agent-less with ``--provider``, ``current_provider``
+ is the TARGET (what switch_model wants), so the launch route names it instead; None when only a
+ credential resolve could tell, which the metric reads as ``unknown`` and so reports the model as
+ ``custom``."""
+ if agent or not explicit_provider:
+ return current_provider
+ if env_provider := os.environ.get("HERMES_TUI_PROVIDER", "").strip():
+ return env_provider
+ if _env_model_seed():
+ return None
+ return _config_model_target()[1] or None
+
+
def _merge_preflight_warning(result, agent, session: dict, cfg, custom_provs) -> None:
"""Fold the context-compression preflight warning into ``result`` (best-effort)."""
try:
@@ -350,8 +364,8 @@ def _apply_model_switch(
from hermes_cli.observability.shared_metrics_events import record_model_switch
record_model_switch(
- from_provider=current_provider, to_provider=result.target_provider, surface=_session_source(session),
- from_model=current_model)
+ from_provider=_switch_away_provider(agent, explicit_provider, current_provider),
+ to_provider=result.target_provider, surface=_session_source(session), from_model=current_model)
return {
"value": result.new_model, "warning": result.warning_message or "",
"confirm_required": False,