Skip to content

The agent that grows with you

fix(tui_gateway): a bare mid-turn model pick gets its confirm before it is queued

Teknium9927e3cd0643
diff --git a/hermes_cli/context_switch_guard.py b/hermes_cli/context_switch_guard.py
index 703c5d07560..3ca8fc5396d 100644
--- a/hermes_cli/context_switch_guard.py
+++ b/hermes_cli/context_switch_guard.py
@@ -45,11 +45,9 @@ def _estimate_tokens(agent: Any, messages: Optional[List[dict]]) -> Optional[int
except Exception:
pass
+ # session_prompt_tokens is a lifetime sum, not occupancy (#126343): never a fallback here.
last = int(getattr(cc, "last_prompt_tokens", 0) or 0)
- if last > 0:
- return last
- session_prompt = int(getattr(agent, "session_prompt_tokens", 0) or 0)
- return session_prompt if session_prompt > 0 else None
+ return last if last > 0 else None
def merge_preflight_compression_warning(
diff --git a/tests/hermes_cli/test_context_switch_guard.py b/tests/hermes_cli/test_context_switch_guard.py
index c1c53ff5539..bf2f584b7be 100644
--- a/tests/hermes_cli/test_context_switch_guard.py
+++ b/tests/hermes_cli/test_context_switch_guard.py
@@ -206,3 +206,13 @@ def test_custom_provider_context_avoids_false_shrink_warning(monkeypatch):
assert "shrinks" in result3.warning_message
# Must not honor the unused 1M custom override when no providers were passed.
assert "1,048,576" not in result3.warning_message
+
+
+def test_lifetime_prompt_counter_is_not_a_size_estimate():
+ """``session_prompt_tokens`` sums every call; without a measured size the guard stays silent (#126343)."""
+ from hermes_cli.context_switch_guard import _estimate_tokens
+
+ agent = SimpleNamespace(context_compressor=SimpleNamespace(last_prompt_tokens=0),
+ session_prompt_tokens=16_282_033)
+
+ assert _estimate_tokens(agent, None) is None
diff --git a/tests/tui_gateway/test_deferred_model_switch_confirm.py b/tests/tui_gateway/test_deferred_model_switch_confirm.py
index 8c598bfecad..59eecbe7975 100644
--- a/tests/tui_gateway/test_deferred_model_switch_confirm.py
+++ b/tests/tui_gateway/test_deferred_model_switch_confirm.py
@@ -206,6 +206,20 @@ class TestLargeContextPickAsksBeforeStashing:
assert result["confirm_required"] is False
assert running_session["pending_model_switch"]["raw"] == UNGUARDED_MODEL
+def test_bare_pick_is_guarded_against_the_live_provider(running_session, monkeypatch):
+ """A pick without --provider resolves against the live provider at turn start, so a
+ provider-keyed price check must see that provider at pick time or it drops the pick later."""
+ from hermes_cli import model_cost_guard
+
+ monkeypatch.setattr(model_cost_guard, "expensive_model_warning", lambda model, provider=None, **_kw: (
+ types.SimpleNamespace(message="pricey") if provider == "openai-api" else None))
+ running_session["agent"] = types.SimpleNamespace(model="gpt-4.1-nano", provider="openai-api")
+
+ result = _config_set_model("o1-pro")["result"]
+
+ assert result["confirm_required"] is True and result["deferred"] is False
+ assert "pending_model_switch" not in running_session
+
class TestDroppedQueuedPickIsLogged:
def test_turn_start_drop_leaves_a_server_log_line(self, monkeypatch, caplog):
session = _session(pending_model_switch={
@@ -248,6 +262,7 @@ class TestDroppedQueuedPickIsNotATurnError:
assert "error" not in kinds
notice = next(a[2] for a in emitted if a[0] == "notification.show")
assert notice["level"] == "warn" and UNGUARDED_MODEL in notice["text"]
+ assert "\n" not in notice["text"], "the guard banner belongs in the re-pick confirm, not the toast"
assert "session.info" in kinds
def test_failed_switch_is_a_warning(self, monkeypatch):
diff --git a/tui_gateway/methods_config_set.py b/tui_gateway/methods_config_set.py
index 83487d9df52..8ca94b2497c 100644
--- a/tui_gateway/methods_config_set.py
+++ b/tui_gateway/methods_config_set.py
@@ -81,8 +81,11 @@ def _stash_pending_model_switch(rid, key, value, session, confirmed, parsed):
pending_model = str(value)
pending_provider = (getattr(parsed, "explicit_provider", "") or "").strip()
if not confirmed:
- pending_warning = _pending_switch_selection_warning(
- pending_model, pending_provider, session.get("agent"))
+ # A bare pick resolves against the live provider at turn start; guard on that same provider
+ # here, or a provider-keyed price check passes now and drops the queued pick later.
+ agent = session.get("agent")
+ guard_provider = pending_provider or (getattr(agent, "provider", "") or "").strip()
+ pending_warning = _pending_switch_selection_warning(pending_model, guard_provider, agent)
if pending_warning is not None:
return _cfgset_model_ok(rid, key, pending_model, pending_warning, pending_warning, deferred=False)
# display_*: _session_info shows the user's pick while pending, not the live old model.
diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.py
index f4203e70c1b..76e869b7f53 100644
--- a/tui_gateway/session_compression.py
+++ b/tui_gateway/session_compression.py
@@ -212,10 +212,12 @@ def _apply_pending_model_switch(sid: str, session: dict) -> None:
# never confirmed.
if not result.get("confirm_required"):
return
- logger.warning("Queued model switch to %s dropped for session %s: selection guard needs a confirm", model, sid)
detail = result.get("confirm_message") or result.get("warning") or ""
+ logger.warning("Queued model switch to %s dropped for session %s: %s", model, sid, detail.split("\n", 1)[0])
current = getattr(session["agent"], "model", "") or "the current model"
- text = f"Stayed on {current}: switching to {model} needs confirmation. Pick it again to confirm.\n\n{detail}"
+ # One line: the TUI status bar truncates and the Desktop toast collapses newlines. The full
+ # guard text comes back in the confirm prompt when the user picks it again.
+ text = f"Stayed on {current}: switching to {model} needs confirmation. Pick it again to confirm."
except Exception as e:
logger.warning("Queued model switch to %s failed for session %s: %s", model, sid, e)
text = f"Could not switch model: {e}"