AI gateway: - Add a generic, admin-selectable `client_profile` field on gateway_providers (e.g. "opencode") so a provider needing special request headers (OpenCode Zen's client-identity spoofing) is configured like any other provider, not hardcoded by name. - Track per-(provider, model) reliability/speed/latency health in memory, seeded from the existing gateway_usage_ledger at startup - purely observational, never influences routing. - New Stats tab on /admin/gateway: request volume, latency, per-model breakdowns, and reliability weight, charted with a vendored Chart.js and devplace's own theme tokens. - Record which model a failed request actually fell back to (fallback_used_route), surfaced in the Recent Failures table. - Stop excluding context_length errors from fallback, and skip a primary attempt outright when its known context window is already too small for the estimated request size, going straight to the fallback. - gateway_usage_ledger's provider/fallback_used_route columns and indexes are ensured centrally in database/schema.py's init_db(), the single point of truth for this table's schema. - Non-OpenAI upstream routing and client-model passthrough; trust only the upstream's own X-Gateway-Model header for served-model attribution. Devii agent: - Fix a real lockup: plan/verify tools could be individually disabled via the admin tool toggles while still being required by the protocol gate, permanently bricking any task that needed tools. They can no longer be disabled, and the gate now also checks the tool is actually offered. - Fix compaction being silently calibrated for a 1M-token model while running a much smaller one: context budget is now percentage-based and the summarizer's own request is sized to fit the real model. - Give a specific, actionable retry message when plan()'s own arguments get cut off by the output limit, and tighten its schema to discourage overlong plans. Other: - Backup service: offload completed backups to a remote Hetzner Storage Box. - Container manager: fix orphan blob leaks from sync races, add a two-phase plan/execute `system prune` CLI command. - Admin gateway UI: replace the JS-rendered model/provider tables with server-rendered forms and pages. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DhmEkvutuwtzFVcLbTrhdo
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import asyncio
|
|
import time
|
|
|
|
from tests.conftest import run_async
|
|
from devplacepy.services.openai_gateway.reliability import (
|
|
_retry_after_seconds,
|
|
retry_send,
|
|
)
|
|
|
|
|
|
class FakeHeaders_reliability(dict):
|
|
def get(self, key, default=None):
|
|
return super().get(key.lower(), default)
|
|
|
|
|
|
class FakeResp_reliability:
|
|
def __init__(self, status_code, retry_after=None):
|
|
self.status_code = status_code
|
|
headers = FakeHeaders_reliability()
|
|
if retry_after is not None:
|
|
headers["retry-after"] = retry_after
|
|
self.headers = headers
|
|
|
|
async def aclose(self):
|
|
pass
|
|
|
|
|
|
def test_retry_after_seconds_parses_numeric_header():
|
|
assert _retry_after_seconds(FakeResp_reliability(429, "2")) == 2.0
|
|
|
|
|
|
def test_retry_after_seconds_absent_returns_none():
|
|
assert _retry_after_seconds(FakeResp_reliability(429)) is None
|
|
|
|
|
|
def test_retry_after_seconds_is_capped():
|
|
assert _retry_after_seconds(FakeResp_reliability(429, "99999")) == 30.0
|
|
|
|
|
|
def test_retry_after_seconds_ignores_garbage():
|
|
assert _retry_after_seconds(FakeResp_reliability(429, "not-a-number-or-date")) is None
|
|
|
|
|
|
def test_retry_send_retries_429_and_honors_retry_after():
|
|
responses = [FakeResp_reliability(429, "0.01"), FakeResp_reliability(200)]
|
|
calls = []
|
|
|
|
async def do_call():
|
|
calls.append(time.monotonic())
|
|
return responses.pop(0)
|
|
|
|
sem = asyncio.Semaphore(1)
|
|
resp, exc, attempts, queue_wait_ms = run_async(
|
|
retry_send(do_call, sem, max_retries=2, backoff_ms=5000)
|
|
)
|
|
assert exc is None
|
|
assert resp.status_code == 200
|
|
assert attempts == 2
|
|
# The 429 branch waits on the short Retry-After (0.01s), never the
|
|
# much larger fixed 5000ms*attempt linear backoff it would otherwise use.
|
|
assert calls[1] - calls[0] < 1.0
|
|
|
|
|
|
def test_retry_send_gives_up_after_max_retries_on_429():
|
|
async def do_call():
|
|
return FakeResp_reliability(429, "0.001")
|
|
|
|
sem = asyncio.Semaphore(1)
|
|
resp, exc, attempts, queue_wait_ms = run_async(
|
|
retry_send(do_call, sem, max_retries=1, backoff_ms=1)
|
|
)
|
|
assert exc is None
|
|
assert resp.status_code == 429
|
|
assert attempts == 2
|