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
This commit is contained in:
2026-09-09 07:38:24 +02:00
co-authored by Claude Sonnet 5
parent b475e7d6ed
commit 569f1dcc64
67 changed files with 6151 additions and 814 deletions
+57 -7
View File
@@ -10,6 +10,11 @@ from typing import Any, Optional
import httpx
from devplacepy.services.openai_gateway.config import VISION_INSTRUCTION
from devplacepy.services.openai_gateway.reliability import (
_backoff,
_is_retryable_status,
_retry_after_seconds,
)
from devplacepy.services.openai_gateway.thinking import apply_thinking
from devplacepy.services.openai_gateway.usage import classify_error
@@ -97,6 +102,9 @@ class VisionAugmenter:
pricing=None,
context_map=None,
app_reference: str = "default",
breaker=None,
max_retries: int = 2,
retry_backoff_ms: int = 250,
):
self.vision_url = vision_url
self.vision_model = vision_model
@@ -110,6 +118,9 @@ class VisionAugmenter:
self.pricing = pricing
self.context_map = context_map or {}
self.app_reference = app_reference
self.breaker = breaker
self.max_retries = max(0, int(max_retries or 0))
self.retry_backoff_ms = max(0, int(retry_backoff_ms or 0))
self.calls = 0
self.cost_usd = 0.0
@@ -142,6 +153,9 @@ class VisionAugmenter:
async def _describe_one(self, client: httpx.AsyncClient, image_block: dict) -> str:
if not self.vision_key:
return "[vision unavailable: vision API key not configured]"
if self.breaker is not None and not self.breaker.allow():
self._record((0.0), 503, False, "circuit_open", None)
return "[vision unavailable: circuit breaker open]"
payload = {
"model": self.vision_model,
"messages": [
@@ -166,18 +180,49 @@ class VisionAugmenter:
if self.title:
headers["X-Title"] = self.title
start = time.monotonic()
try:
resp = await client.post(self.vision_url, json=payload, headers=headers)
except httpx.RequestError as e:
logger.warning("vision connection failed: %s", e)
attempt = 0
resp: Optional[httpx.Response] = None
exc: Optional[Exception] = None
while True:
attempt += 1
try:
resp = await client.post(self.vision_url, json=payload, headers=headers)
exc = None
except httpx.RequestError as e:
resp = None
exc = e
if exc is None and not _is_retryable_status(resp.status_code):
break
if attempt > self.max_retries:
break
retry_after = _retry_after_seconds(resp) if resp is not None else None
logger.info(
"vision call retrying (%d/%d): %s",
attempt,
self.max_retries,
exc or f"HTTP {resp.status_code}",
)
if resp is not None:
try:
await resp.aclose()
except Exception: # noqa: BLE001
pass
await _backoff(self.retry_backoff_ms, attempt, retry_after)
if self.breaker is not None:
if exc is not None or (resp is not None and resp.status_code >= 500):
self.breaker.record_failure()
else:
self.breaker.record_success()
if exc is not None:
logger.warning("vision connection failed: %s", exc)
self._record(
(time.monotonic() - start) * 1000,
502,
False,
classify_error(0, e),
classify_error(0, exc),
None,
)
return f"[vision call failed: {e}]"
return f"[vision call failed: {exc}]"
latency_ms = (time.monotonic() - start) * 1000
if resp.status_code != 200:
logger.warning("vision %s: %s", resp.status_code, resp.text[:200])
@@ -191,10 +236,15 @@ class VisionAugmenter:
return f"[vision failed: HTTP {resp.status_code}]"
try:
data = resp.json()
error = data.get("error") if isinstance(data, dict) else None
if error and "choices" not in data:
message = error.get("message") if isinstance(error, dict) else str(error)
self._record(latency_ms, 502, False, "upstream_error", None)
return f"[vision failed: {message}]"
text = data["choices"][0]["message"].get("content") or ""
self._record(latency_ms, 200, True, None, data.get("usage"))
return text.strip() or "[vision returned empty response]"
except (KeyError, IndexError, ValueError) as e:
except (KeyError, IndexError, ValueError, httpx.HTTPError) as e:
self._record(latency_ms, 200, False, "gateway", None)
return f"[vision parse error: {e}]"