Skip to content

The agent that grows with you

fix(tui_gateway): adopt fallback_providers edits into open Desktop/TUI chats at turn start

teknium1adc3698bce0b
diff --git a/tests/tui_gateway/test_fallback_chain_hot_reload.py b/tests/tui_gateway/test_fallback_chain_hot_reload.py
new file mode 100644
index 00000000000..fd7d4e71cb9
--- /dev/null
+++ b/tests/tui_gateway/test_fallback_chain_hot_reload.py
@@ -0,0 +1,43 @@
+"""Desktop/TUI sessions must adopt a ``fallback_providers`` chain added after the chat was opened.
+
+Regression for #95066: ``_make_agent`` read the chain once, so a session born before ``hermes fallback
+add`` kept an empty ``_fallback_chain`` forever and a Codex ``usage_limit_reached`` 429 ended in a
+provider error instead of switching to the configured fallback.
+"""
+
+from __future__ import annotations
+
+import inspect
+from types import SimpleNamespace
+
+from tui_gateway import server
+
+
+def _session(chain=None):
+ agent = SimpleNamespace(
+ _fallback_chain=list(chain or []), _fallback_model=None, _fallback_index=0,
+ _fallback_activated=False, _rate_limited_until=0, _unavailable_fallback_keys=set(),
+ )
+ return {"agent": agent, "session_key": "session-95066"}, agent
+
+
+def test_chain_added_after_open_reaches_the_live_agent(monkeypatch):
+ session, agent = _session()
+ fallback = [{"provider": "xai-oauth", "model": "grok-4.6"}]
+ monkeypatch.setattr(server, "_load_cfg", lambda: {"fallback_providers": fallback})
+
+ server._sync_agent_fallback_with_config("sid", session)
+
+ assert agent._fallback_chain == fallback
+ assert agent._fallback_model == fallback[0]
+ assert agent._fallback_index == 0
+
+
+def test_turn_admission_syncs_fallback_chain_before_running():
+ # Wiring guard: the sync must run on the turn thread right beside the model/compression syncs.
+ from tui_gateway import prompt_turn
+
+ source = inspect.getsource(prompt_turn)
+ sync_idx = source.find("_sync_agent_fallback_with_config(sid, session)")
+ assert sync_idx > 0
+ assert source.find("_sync_agent_compression_with_config(sid, session)") < sync_idx < source.find("st.agent = agent = session[\"agent\"]")
diff --git a/tui_gateway/agent_callbacks.py b/tui_gateway/agent_callbacks.py
index adc2085c2be..2141feb4e7e 100644
--- a/tui_gateway/agent_callbacks.py
+++ b/tui_gateway/agent_callbacks.py
@@ -310,6 +310,24 @@ def _load_fallback_model():
return get_fallback_chain(_load_cfg())
+def _sync_agent_fallback_with_config(sid: str, session: dict) -> None:
+ """Adopt ``fallback_providers`` edits into the cached agent at turn start.
+
+ Desktop/TUI chats keep one agent across turns, and ``_make_agent`` reads the chain once: a chat
+ opened before ``hermes fallback add`` kept an empty chain forever and a provider-quota 429 ended in
+ a provider error with a healthy fallback configured (#95066). Same per-turn contract the messaging
+ gateway applies to its cached agents; fail-open so a torn config read never blocks the turn.
+ """
+ agent = session.get("agent")
+ if agent is None:
+ return
+ try:
+ from gateway.run import GatewayRunner
+ GatewayRunner._apply_fallback_chain_to_agent(agent, _load_fallback_model())
+ except Exception as e:
+ logger.warning("fallback chain sync failed for %s: %s", sid, e)
+
+
def _background_agent_kwargs(agent, task_id: str) -> dict:
cfg = _load_cfg()
diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.py
index b06a99dade9..748692f700c 100644
--- a/tui_gateway/prompt_turn.py
+++ b/tui_gateway/prompt_turn.py
@@ -519,6 +519,7 @@ def _prepare_turn_input(sid: str, session: dict, st: _TurnRun, text: Any, images
_apply_pending_model_switch(sid, session)
_sync_agent_model_with_config(sid, session)
_sync_agent_compression_with_config(sid, session)
+ _sync_agent_fallback_with_config(sid, session) # chain added after the chat opened reaches this turn
_sync_bot_capabilities(sid, session) # Bot Chat: adopt Settings->Capabilities edits
st.agent = agent = session["agent"]
# Snapshot after the model sync: a deferred switch's history mutation belongs to this turn.
diff --git a/website/docs/user-guide/features/fallback-providers.md b/website/docs/user-guide/features/fallback-providers.md
index d8cf8f73af8..054dbdae820 100644
--- a/website/docs/user-guide/features/fallback-providers.md
+++ b/website/docs/user-guide/features/fallback-providers.md
@@ -188,6 +188,7 @@ fallback_providers:
|---------|-------------------|
| CLI sessions | ✔ |
| Messaging gateway (Telegram, Discord, etc.) | ✔ |
+| Desktop app / TUI chats | ✔ (a chain added or edited while a chat is open applies from its next turn) |
| Subagent delegation | ✔ (`delegation.fallback_providers` when set; otherwise only unpinned children inherit the parent chain; `[]` disables) |
| Cron jobs | ✔ (cron agents inherit configured fallback providers) |
| Auxiliary tasks on `provider: auto` | ✔ (try per-task fallback, then the main fallback chain before built-in aux discovery) |