Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions run_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2049,12 +2049,14 @@ def switch_model(self, new_model, new_provider, api_key='', base_url='', api_mod
# ("switched to anthropic, tui keeps trying openrouter").
old_norm = (old_provider or "").strip().lower()
new_norm = (new_provider or "").strip().lower()
fallback_chain = list(getattr(self, "_fallback_chain", []) or [])
if old_norm and new_norm and old_norm != new_norm:
self._fallback_chain = [
entry for entry in self._fallback_chain
fallback_chain = [
entry for entry in fallback_chain
if (entry.get("provider") or "").strip().lower() not in {old_norm, new_norm}
]
self._fallback_model = self._fallback_chain[0] if self._fallback_chain else None
self._fallback_chain = fallback_chain
self._fallback_model = fallback_chain[0] if fallback_chain else None

logging.info(
"Model switched in-place: %s (%s) -> %s (%s)",
Expand Down
11 changes: 11 additions & 0 deletions tests/run_agent/test_switch_model_fallback_prune.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ def test_switch_with_empty_chain_stays_empty():
assert agent._fallback_model is None


def test_switch_initializes_missing_fallback_attrs():
agent = _make_agent([])
del agent._fallback_chain
del agent._fallback_model

_switch_to_anthropic(agent)

assert agent._fallback_chain == []
assert agent._fallback_model is None


def test_switch_within_same_provider_preserves_chain():
chain = [{"provider": "openrouter", "model": "x-ai/grok-4"}]
agent = _make_agent(chain)
Expand Down
Loading