refactor(adaptive_router): move update_queue out of litellm.proxy
Some checks failed
Unit Tests: Proxy DB Operations / proxy-db (auth-checks, tests/proxy_unit_tests/test_auth_checks.py tests/proxy_unit_tests/test_user_api_key_auth.py, 20, 8) (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-db (key-generation, tests/proxy_unit_tests/test_key_generate_prisma.py, 30, 0) (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-db (proxy-utils, tests/proxy_unit_tests/test_proxy_utils.py, 20, 8) (push) Has been cancelled
Unit Tests: Proxy DB Operations / proxy-db (remaining, tests/proxy_unit_tests --ignore=tests/proxy_unit_tests/test_key_generate_prisma.py --ignore=tests/proxy_unit_tests/test_auth_checks.py --ignore=tests/proxy_unit_tests/test_user_api_key_auth.py --ignore=tests/proxy_unit_tests/test_p… (push) Has been cancelled
Unit Tests: Security / security (push) Has been cancelled

P1 review: adaptive_router.py had a top-level import of
AdaptiveRouterUpdateQueue from litellm.proxy.db, which broke the
SDK/proxy boundary that every other router strategy respects. No
other router_strategy module imports from litellm.proxy at module
level.

The queue only depends on litellm._logging — it never needed to
live under litellm.proxy. Moved:

  litellm/proxy/db/db_transaction_queue/adaptive_router_update_queue.py
  → litellm/router_strategy/adaptive_router/update_queue.py

  tests/test_litellm/proxy/db/db_transaction_queue/
    test_adaptive_router_update_queue.py
  → tests/test_litellm/router_strategy/adaptive_router/test_update_queue.py

Also switched the queue's logger from verbose_proxy_logger to
verbose_router_logger to match the new module's ownership.

P2 review: drop unused constant STAGNATION_JACCARD_EXACT from
config.py — it was defined but never referenced.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Krrish Dholakia 2026-04-21 18:02:17 -07:00
parent 1965c67e8f
commit e50f945ef7
4 changed files with 13 additions and 14 deletions

View file

@ -27,9 +27,6 @@ from litellm._logging import verbose_router_logger
from litellm.litellm_core_utils.prompt_templates.common_utils import (
get_last_user_message,
)
from litellm.proxy.db.db_transaction_queue.adaptive_router_update_queue import (
AdaptiveRouterUpdateQueue,
)
from litellm.router_strategy.adaptive_router.bandit import (
BanditCell,
apply_delta,
@ -43,18 +40,21 @@ from litellm.router_strategy.adaptive_router.config import (
MIN_QUALITY_TIER_METADATA_KEY,
OWNER_CACHE_TTL_SECONDS,
)
# Sweep session-state cache when it exceeds this many live entries. Expired
# entries are dropped in bulk; amortizes to O(1) per insert.
_SESSION_STATE_SWEEP_THRESHOLD: int = 1024
# Same pattern for the owner cache.
_OWNER_CACHE_SWEEP_THRESHOLD: int = 1024
from litellm.router_strategy.adaptive_router.signals import (
SessionState,
SignalDelta,
Turn,
apply_turn,
)
from litellm.router_strategy.adaptive_router.update_queue import (
AdaptiveRouterUpdateQueue,
)
# Sweep session-state cache when it exceeds this many live entries. Expired
# entries are dropped in bulk; amortizes to O(1) per insert.
_SESSION_STATE_SWEEP_THRESHOLD: int = 1024
# Same pattern for the owner cache.
_OWNER_CACHE_SWEEP_THRESHOLD: int = 1024
from litellm.types.llms.openai import AllMessageValues
from litellm.types.router import (
AdaptiveRouterConfig,

View file

@ -39,7 +39,6 @@ SIGNAL_GATE_MIN_MESSAGES: int = 4
# Detector thresholds (from Plano/Chen 2026 paper).
MISALIGNMENT_JACCARD_THRESHOLD: float = 0.45
STAGNATION_JACCARD_NEAR_DUP: float = 0.50
STAGNATION_JACCARD_EXACT: float = 0.85
LOOP_REPEAT_THRESHOLD: int = 3
TOOL_CALL_HISTORY_MAX: int = 20

View file

@ -21,7 +21,7 @@ from __future__ import annotations
import asyncio
from typing import Any, Dict, Tuple
from litellm._logging import verbose_proxy_logger
from litellm._logging import verbose_router_logger
StateKey = Tuple[str, str, str] # (router_name, request_type, model_name)
SessionKey = Tuple[str, str, str] # (session_id, router_name, model_name)
@ -139,7 +139,7 @@ class AdaptiveRouterUpdateQueue:
},
)
except Exception as e:
verbose_proxy_logger.exception(
verbose_router_logger.exception(
"AdaptiveRouterUpdateQueue: failed to flush state for %s: %s",
key,
e,
@ -193,7 +193,7 @@ class AdaptiveRouterUpdateQueue:
},
)
except Exception as e:
verbose_proxy_logger.exception(
verbose_router_logger.exception(
"AdaptiveRouterUpdateQueue: failed to flush session for %s: %s",
key,
e,

View file

@ -3,7 +3,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from litellm.proxy.db.db_transaction_queue.adaptive_router_update_queue import (
from litellm.router_strategy.adaptive_router.update_queue import (
AdaptiveRouterUpdateQueue,
)