mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-10-10 17:02:20 +02:00
refactor(model-routing): centralize explicit foreground fallback policy (#6020)
* refactor(model-routing): centralize explicit foreground fallback policy Make foreground fallback an explicit per-user, availability-only policy shared by streaming Chat, non-stream Chat, and Agent runs. Preserve strict defaults, owner/model and credential boundaries, pinned Agent routes, and truthful per-round provenance/accounting. Carry provider-reported model identifiers through native streaming adapters, non-stream responses, and caches, and keep legacy default_model_fallbacks as tombstoned raw storage that generic settings APIs and agent tools cannot expose or mutate. * fix(agent-loop): restore rebase-dropped qwen routing, workspace prompt, and temperature clamp * fix(model-routing): thread selected endpoint identity, fix cost classification and fallback eligibility * fix(chat): restore stream helpers and harden run stop lifecycle * fix(model-routing): let numeric provider codes win over symbolic rate-limit statuses * fix(agent-loop): apply qwen temperature and notes-tool clamps per fallback candidate * fix(chat): honor queued stop across resend and reload canonical terminal on EOF * fix(chat): track stop queue and cleanup ownership by per-send generation * fix(agent-loop): preserve requested temperature for non-qwen fallback candidates * fix(chat): reserve send ownership before any await and scope stop to the current send * fix(chat): clear the previous run identity at send reservation --------- Co-authored-by: RaresKeY <158580472+RaresKeY@users.noreply.github.com> Co-authored-by: StressTestor <212606152+StressTestor@users.noreply.github.com>
This commit is contained in:
co-authored by
RaresKeY
StressTestor
parent
b52296471b
commit
c4369305f0
@@ -3,8 +3,16 @@
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
import src.endpoint_resolver as endpoint_resolver
|
||||
from src.endpoint_resolver import resolve_endpoint
|
||||
from src.endpoint_resolver import (
|
||||
endpoint_cost_tracked,
|
||||
resolve_endpoint,
|
||||
resolve_endpoint_by_id,
|
||||
resolve_fallback_entries,
|
||||
resolve_fallback_entries_with_descriptors,
|
||||
)
|
||||
|
||||
|
||||
class _FakeColumn:
|
||||
@@ -34,6 +42,9 @@ class _FakeQuery:
|
||||
def first(self):
|
||||
return self.rows[0] if self.rows else None
|
||||
|
||||
def all(self):
|
||||
return list(self.rows)
|
||||
|
||||
|
||||
class _FakeDb:
|
||||
def __init__(self, rows):
|
||||
@@ -49,6 +60,7 @@ class _FakeDb:
|
||||
def _endpoint(ep_id, model, *, hidden=None):
|
||||
return SimpleNamespace(
|
||||
id=ep_id,
|
||||
name=f"Endpoint {ep_id}",
|
||||
base_url=f"https://{ep_id}.example/v1",
|
||||
api_key=f"key-{ep_id}",
|
||||
cached_models=json.dumps([model]),
|
||||
@@ -191,3 +203,140 @@ def test_hidden_configured_model_selects_first_enabled_chat_model(monkeypatch):
|
||||
assert url == "https://default.example/v1/chat/completions"
|
||||
assert model == "enabled-chat"
|
||||
assert headers == {"Authorization": "Bearer key-default"}
|
||||
|
||||
|
||||
def test_exact_fallback_drops_hidden_model_instead_of_substituting(monkeypatch):
|
||||
endpoint = SimpleNamespace(
|
||||
id="fallback",
|
||||
base_url="https://fallback.example/v1",
|
||||
api_key="key-fallback",
|
||||
cached_models=json.dumps(["chosen-hidden", "different-live"]),
|
||||
hidden_models=json.dumps(["chosen-hidden"]),
|
||||
is_enabled=True,
|
||||
)
|
||||
_install_resolver_fakes(monkeypatch, {}, [endpoint])
|
||||
|
||||
assert resolve_endpoint_by_id(
|
||||
"fallback",
|
||||
"chosen-hidden",
|
||||
require_exact_model=True,
|
||||
) is None
|
||||
assert resolve_endpoint_by_id("fallback", "chosen-hidden")[1] == "different-live"
|
||||
|
||||
|
||||
def test_exact_fallback_drops_known_missing_model(monkeypatch):
|
||||
_install_resolver_fakes(monkeypatch, {}, [_endpoint("fallback", "known-live")])
|
||||
|
||||
assert resolve_endpoint_by_id(
|
||||
"fallback",
|
||||
"unlisted-model",
|
||||
require_exact_model=True,
|
||||
) is None
|
||||
|
||||
|
||||
def test_fallback_entry_resolution_preserves_credential_distinct_endpoints(monkeypatch):
|
||||
seen = []
|
||||
|
||||
def fake_resolve(ep_id, model, owner=None, *, require_exact_model=False):
|
||||
seen.append((ep_id, model, owner, require_exact_model))
|
||||
return (
|
||||
"https://provider.example/v1/chat/completions",
|
||||
model,
|
||||
{"Authorization": f"Bearer {ep_id}"},
|
||||
)
|
||||
|
||||
monkeypatch.setattr(endpoint_resolver, "resolve_endpoint_by_id", fake_resolve)
|
||||
entries = [
|
||||
{"endpoint_id": "key-one", "model": "same-model"},
|
||||
{"endpoint_id": "key-two", "model": "same-model"},
|
||||
]
|
||||
|
||||
assert resolve_fallback_entries(
|
||||
entries,
|
||||
owner="alice",
|
||||
require_exact_model=True,
|
||||
) == [
|
||||
("https://provider.example/v1/chat/completions", "same-model", {"Authorization": "Bearer key-one"}),
|
||||
("https://provider.example/v1/chat/completions", "same-model", {"Authorization": "Bearer key-two"}),
|
||||
]
|
||||
assert seen == [
|
||||
("key-one", "same-model", "alice", True),
|
||||
("key-two", "same-model", "alice", True),
|
||||
]
|
||||
|
||||
|
||||
def test_descriptor_resolution_preserves_safe_endpoint_identity(monkeypatch):
|
||||
_install_resolver_fakes(monkeypatch, {}, [_endpoint("backup", "backup-model")])
|
||||
|
||||
routes = resolve_fallback_entries_with_descriptors(
|
||||
[{"endpoint_id": "backup", "model": "backup-model"}],
|
||||
require_exact_model=True,
|
||||
)
|
||||
|
||||
assert routes == [(
|
||||
(
|
||||
"https://backup.example/v1/chat/completions",
|
||||
"backup-model",
|
||||
{"Authorization": "Bearer key-backup"},
|
||||
),
|
||||
{
|
||||
"endpoint_id": "backup",
|
||||
"endpoint_label": "Endpoint backup",
|
||||
"endpoint_cost_tracked": True,
|
||||
},
|
||||
)]
|
||||
|
||||
|
||||
def test_exact_id_descriptor_wins_when_routes_are_identical(monkeypatch):
|
||||
first = _endpoint("account-one", "same-model")
|
||||
second = _endpoint("account-two", "same-model")
|
||||
for endpoint in (first, second):
|
||||
endpoint.base_url = "https://provider.example/v1"
|
||||
endpoint.api_key = "shared-key"
|
||||
_install_resolver_fakes(monkeypatch, {}, [first, second])
|
||||
|
||||
import src.auth_helpers as auth_helpers
|
||||
|
||||
seen_owners = []
|
||||
|
||||
def scoped(query, model_cls, owner, *, include_shared=True):
|
||||
seen_owners.append(owner)
|
||||
return query
|
||||
|
||||
monkeypatch.setattr(auth_helpers, "owner_filter", scoped)
|
||||
resolver = getattr(endpoint_resolver, "resolve_route_descriptor_by_id", None)
|
||||
|
||||
assert resolver is not None
|
||||
assert resolver(
|
||||
"account-two",
|
||||
"https://provider.example/v1/chat/completions",
|
||||
"same-model",
|
||||
{"Authorization": "Bearer shared-key"},
|
||||
owner="alice",
|
||||
) == {
|
||||
"endpoint_id": "account-two",
|
||||
"endpoint_label": "Endpoint account-two",
|
||||
"endpoint_cost_tracked": True,
|
||||
}
|
||||
assert seen_owners == ["alice"]
|
||||
|
||||
|
||||
def test_endpoint_cost_tracking_is_non_secret_route_classification():
|
||||
assert endpoint_cost_tracked("http://localhost:11434/v1") is False
|
||||
assert endpoint_cost_tracked("http://model-service:8000/v1") is False
|
||||
assert endpoint_cost_tracked("http://192.168.1.20:8000/v1") is False
|
||||
assert endpoint_cost_tracked("https://chatgpt.com/backend-api/codex") is False
|
||||
assert endpoint_cost_tracked("https://api.example.com/v1") is True
|
||||
assert endpoint_cost_tracked("http://192.168.1.20:8000/v1", "api") is True
|
||||
assert endpoint_cost_tracked("https://api.example.com/v1", "local") is False
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("url", "expected"),
|
||||
[
|
||||
("https://[2606:4700:4700::1111]/v1", True),
|
||||
("http://169.254.10.20:8000/v1", False),
|
||||
],
|
||||
)
|
||||
def test_endpoint_cost_tracking_classifies_public_ipv6_and_link_local_ipv4(url, expected):
|
||||
assert endpoint_cost_tracked(url) is expected
|
||||
|
||||
Reference in New Issue
Block a user