Skip to content

The agent that grows with you

fix(tui_gateway): a model switch dropped at turn start warns instead of failing the turn

Brooklyn Nicholsonb1f53dd8ad71
diff --git a/tests/tui_gateway/test_deferred_model_switch_confirm.py b/tests/tui_gateway/test_deferred_model_switch_confirm.py
index dd543166199..8c598bfecad 100644
--- a/tests/tui_gateway/test_deferred_model_switch_confirm.py
+++ b/tests/tui_gateway/test_deferred_model_switch_confirm.py
@@ -223,5 +223,38 @@ class TestDroppedQueuedPickIsLogged:
assert "dropped" in caplog.text and UNGUARDED_MODEL in caplog.text, (
"a dropped pick used to exist only as a client toast, invisible in server logs"
)
- assert emitted and emitted[0][0] == "error"
assert "pending_model_switch" not in session
+
+
+class TestDroppedQueuedPickIsNotATurnError:
+ """A pick dropped at turn start must not fail the turn: Desktop paints ``error`` as the red retry card
+ under the user's message, while the turn itself runs on the old model."""
+
+ def _run(self, monkeypatch, apply):
+ session = _session(pending_model_switch={
+ "raw": UNGUARDED_MODEL, "confirm_expensive_model": False,
+ "display_model": UNGUARDED_MODEL, "display_provider": ""})
+ session["agent"] = types.SimpleNamespace(model="deepseek/deepseek-v4.1-flash")
+ monkeypatch.setattr(server, "_apply_model_switch", apply)
+ emitted = []
+ monkeypatch.setattr(server, "_emit", lambda *a, **_kw: emitted.append(a))
+ server._apply_pending_model_switch("sid", session)
+ return [a[0] for a in emitted], emitted
+
+ def test_guarded_drop_is_a_warning_and_resyncs_the_pill(self, monkeypatch):
+ kinds, emitted = self._run(
+ monkeypatch, lambda *_a, **_kw: {"confirm_required": True, "confirm_message": "guarded"})
+
+ 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 "session.info" in kinds
+
+ def test_failed_switch_is_a_warning(self, monkeypatch):
+ def _boom(*_a, **_kw):
+ raise ValueError("no credentials")
+
+ kinds, _ = self._run(monkeypatch, _boom)
+
+ assert "error" not in kinds
+ assert "notification.show" in kinds
diff --git a/tui_gateway/session_compression.py b/tui_gateway/session_compression.py
index 9c79c922e94..3aa7412336a 100644
--- a/tui_gateway/session_compression.py
+++ b/tui_gateway/session_compression.py
@@ -197,20 +197,32 @@ def _sync_agent_compression_with_config(sid: str, session: dict) -> None:
def _apply_pending_model_switch(sid: str, session: dict) -> None:
"""Apply a model switch queued (``session["pending_model_switch"]``) while a turn was running. Runs on
the TURN thread at turn start — nothing in flight — so the in-place swap (client rebuild) is safe. A
- failed switch keeps the current model and never blocks the turn."""
+ failed switch keeps the current model and never blocks the turn.
+
+ A dropped pick is a warning notice, not an ``error``: clients paint ``error`` as a failed turn
+ (Desktop's red retry card under the user's message) while this turn runs normally on the old model.
+ The session.info re-sync moves the pill off the pick the session never adopted."""
pending = session.pop("pending_model_switch", None)
if not pending or session.get("agent") is None:
return
+ model = pending.get("display_model") or pending["raw"]
try:
result = _apply_model_switch(sid, session, pending["raw"], confirm_expensive_model=bool(pending.get("confirm_expensive_model")))
- # Honour the expensive-model confirm: surface the warning and drop the switch rather than spend
- # on a model the user never confirmed.
- if result.get("confirm_required"):
- logger.warning("Queued model switch to %s dropped for session %s: selection guard needs a confirm",
- pending.get("display_model") or pending["raw"], sid)
- _emit("error", sid, {"message": result.get("confirm_message") or result.get("warning") or ""})
+ # Honour the expensive-model confirm: drop the switch rather than spend on a model the user
+ # 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 ""
+ 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}"
except Exception as e:
- _emit("error", sid, {"message": f"Could not switch model: {e}"})
+ logger.warning("Queued model switch to %s failed for session %s: %s", model, sid, e)
+ text = f"Could not switch model: {e}"
+ _emit("notification.show", sid, {
+ "text": text.rstrip(), "level": "warn", "kind": "ttl", "ttl_ms": 12000,
+ "key": "model_switch.dropped", "id": "model_switch.dropped"})
+ _emit_session_info(sid, session)
class CompressionLockHeld(Exception):