Files
devplacepy/tests/api/admin/gateway/index.py
T
retoorandClaude Sonnet 5 569f1dcc64 Add OpenCode Zen support, model health/stats dashboard, and gateway fallback fixes
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
2026-09-09 07:38:24 +02:00

98 lines
2.9 KiB
Python

# retoor <retoor@molodetz.nl>
import time
import requests
from devplacepy.database import get_table, refresh_snapshot
from tests.conftest import BASE_URL
JSON_gateway = {"Accept": "application/json"}
_counter_gateway = [0]
def _db_user_gateway(name):
refresh_snapshot()
return get_table("users").find_one(username=name)
def _unique_gateway(prefix="gw"):
_counter_gateway[0] += 1
return f"{prefix}{int(time.time() * 1000)}{_counter_gateway[0]}"
def admin_session(seeded_db):
session = requests.Session()
session.headers.update(
{"X-API-KEY": _db_user_gateway("alice_test")["api_key"], **JSON_gateway}
)
return session
def member_key():
name = _unique_gateway("gwmem")
requests.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
return _db_user_gateway(name)["api_key"]
def test_gateway_page_requires_admin(seeded_db):
assert (
requests.get(
f"{BASE_URL}/admin/gateway", headers=JSON_gateway, allow_redirects=False
).status_code
== 401
)
key = member_key()
assert (
requests.get(
f"{BASE_URL}/admin/gateway",
headers={**JSON_gateway, "X-API-KEY": key},
allow_redirects=False,
).status_code
== 403
)
def test_gateway_page_renders_for_admin(seeded_db):
admin = admin_session(seeded_db)
response = admin.get(f"{BASE_URL}/admin/gateway", headers={"Accept": "text/html"})
assert response.status_code == 200
assert "Gateway routing" in response.text
def test_gateway_page_tabs_default_and_select_content(seeded_db):
admin = admin_session(seeded_db)
default_page = admin.get(f"{BASE_URL}/admin/gateway", headers={"Accept": "text/html"})
assert "Model routes" in default_page.text
assert "GatewayAdmin" not in default_page.text
providers_page = admin.get(f"{BASE_URL}/admin/gateway?tab=providers", headers={"Accept": "text/html"})
assert "Providers" in providers_page.text
assert "Model routes" not in providers_page.text
quota_page = admin.get(f"{BASE_URL}/admin/gateway?tab=quota", headers={"Accept": "text/html"})
assert "Quota rules" in quota_page.text
assert "Model routes" not in quota_page.text
unknown_tab_page = admin.get(f"{BASE_URL}/admin/gateway?tab=bogus", headers={"Accept": "text/html"})
assert "Model routes" in unknown_tab_page.text
def test_gateway_page_json_reports_active_tab(seeded_db):
admin = admin_session(seeded_db)
response = admin.get(f"{BASE_URL}/admin/gateway?tab=providers")
assert response.status_code == 200
assert response.json()["tab"] == "providers"