Files
DeepTutor/tests/services/model_selection/test_fallbacks.py
yaqi d5574b2c51 feat(chat): name the failing provider and the unused alternatives
Part of #2693, visibility only: the turn still runs the one active model, but a
failed turn no longer reads as "the provider connection was interrupted" with
no name, no attempt count, and no hint that an alternative is already
configured.

- `provider_visibility.py` builds the retry and final-failure copy from a
  pipeline's binding, model and endpoint host, plus the number of configured
  fallbacks (`services/model_selection/fallbacks.py`: read-only, bounded,
  tolerant of a hand-edited catalog).
- Non-transport provider errors (401/403/429/5xx, model-not-found) are mapped
  through the existing error taxonomy before they are raised, so the user reads
  "provider X returned a server-side error" instead of the SDK's raw
  "Error code: 502" — while the typed error, status code and retry flags that
  the loop and the forced-finish path branch on are preserved.
- The failure trace metadata gains `provider`, `attempts` and
  `fallbacks_configured`.
- zh and en chat prompt copy updated; other capabilities keep the code defaults
  until their prompt packs are updated.

No failover yet: switching, cooldowns and failback land in P1.
2026-11-24 15:27:37 +08:00

75 lines
2.3 KiB
Python

"""Read-only fallback roster used by the provider-failure copy (#2693 P0)."""
from __future__ import annotations
from deeptutor.services.model_selection.fallbacks import (
MAX_FALLBACKS,
configured_fallbacks,
fallback_count,
)
def _catalog(entries) -> dict:
return {"services": {"llm": {"fallbacks": entries}}}
def test_missing_key_means_no_fallbacks() -> None:
assert configured_fallbacks({}) == []
assert configured_fallbacks({"services": {}}) == []
assert configured_fallbacks({"services": {"llm": {}}}) == []
assert fallback_count({}) == 0
def test_reads_profile_and_model_pairs_in_order() -> None:
entries = [
{"profile_id": "local-vllm", "model_id": "qwen3-32b"},
{"profile_id": "deepseek", "model_id": "deepseek-chat"},
]
assert configured_fallbacks(_catalog(entries)) == entries
assert fallback_count(_catalog(entries)) == 2
def test_drops_malformed_and_duplicate_entries() -> None:
entries = [
{"profile_id": "local-vllm", "model_id": "qwen3-32b"},
{"profile_id": "local-vllm", "model_id": "qwen3-32b"},
{"profile_id": "", "model_id": "x"},
{"profile_id": "x"},
"not-a-dict",
None,
{"profile_id": " deepseek ", "model_id": " deepseek-chat "},
]
assert configured_fallbacks(_catalog(entries)) == [
{"profile_id": "local-vllm", "model_id": "qwen3-32b"},
{"profile_id": "deepseek", "model_id": "deepseek-chat"},
]
def test_bounded_to_the_planned_chain_cap() -> None:
entries = [{"profile_id": f"p{i}", "model_id": f"m{i}"} for i in range(MAX_FALLBACKS + 3)]
assert len(configured_fallbacks(_catalog(entries))) == MAX_FALLBACKS
def test_non_list_value_is_ignored() -> None:
# A hand-edited file can hold anything; an error path must not care.
assert configured_fallbacks(_catalog({"profile_id": "x"})) == []
assert configured_fallbacks(_catalog("nonsense")) == []
def test_unreadable_catalog_degrades_to_zero(monkeypatch) -> None:
import deeptutor.services.model_selection.fallbacks as fallbacks
class _Broken:
def load(self):
raise RuntimeError("catalog is not initialised")
monkeypatch.setattr(
"deeptutor.services.config.get_model_catalog_service",
lambda: _Broken(),
)
assert fallbacks.configured_fallbacks() == []