Files
devplacepy/tests/unit/services/openai_gateway/model_health.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

84 lines
3.1 KiB
Python

# retoor <retoor@molodetz.nl>
from devplacepy.services.openai_gateway import model_health
def setup_function():
model_health.reset()
def test_speed_reward_is_neutral_without_data():
assert model_health.speed_reward(None) == model_health.NEUTRAL_REWARD
assert model_health.speed_reward(0) == model_health.NEUTRAL_REWARD
def test_speed_reward_increases_with_throughput():
slow = model_health.speed_reward(5)
fast = model_health.speed_reward(50)
assert 0 < slow < fast < 1
def test_latency_reward_is_one_without_data():
assert model_health.latency_reward(None) == 1.0
assert model_health.latency_reward(0) == 1.0
def test_latency_reward_decreases_with_latency():
fast = model_health.latency_reward(500)
slow = model_health.latency_reward(20000)
assert 0 < slow < fast <= 1
def test_untested_model_has_neutral_weight():
health = model_health.ModelHealth()
assert health.weight() == 0.5
def test_record_outcome_success_improves_weight_over_failures():
model_health.record_outcome("openrouter", "fast-model", True, latency_ms=200, tokens_per_second=80)
model_health.record_outcome("openrouter", "slow-model", False)
fast_weight = model_health.snapshot_for("openrouter", "fast-model")["weight"]
slow_weight = model_health.snapshot_for("openrouter", "slow-model")["weight"]
assert fast_weight > 0.5
assert slow_weight < 0.5
def test_repeated_failures_open_the_circuit_for_display_only():
for _ in range(model_health.CIRCUIT_BREAKER_FAILURE_THRESHOLD):
model_health.record_outcome("deepseek", "flaky-model", False)
snapshot = model_health.snapshot_for("deepseek", "flaky-model")
assert snapshot["circuit_open"] is True
assert snapshot["consecutive_failures"] == model_health.CIRCUIT_BREAKER_FAILURE_THRESHOLD
def test_success_resets_consecutive_failures_and_circuit():
model_health.record_outcome("deepseek", "recovering-model", False)
model_health.record_outcome("deepseek", "recovering-model", False)
model_health.record_outcome("deepseek", "recovering-model", True, latency_ms=100, tokens_per_second=50)
snapshot = model_health.snapshot_for("deepseek", "recovering-model")
assert snapshot["consecutive_failures"] == 0
assert snapshot["circuit_open"] is False
def test_snapshot_for_unknown_model_is_none():
assert model_health.snapshot_for("openrouter", "never-seen") is None
def test_apply_history_accumulates_without_touching_circuit_state():
model_health.apply_history("openrouter", "seeded-model", success_count=10, failure_count=2, total_reward=6.0)
snapshot = model_health.snapshot_for("openrouter", "seeded-model")
assert snapshot["success_count"] == 10
assert snapshot["failure_count"] == 2
assert snapshot["circuit_open"] is False
def test_record_outcome_requires_a_model_name():
model_health.record_outcome("openrouter", "", True, latency_ms=100)
assert model_health.snapshot_all() == {}
def test_snapshot_all_keys_are_provider_colon_model():
model_health.record_outcome("groq", "llama-3", True)
keys = model_health.snapshot_all().keys()
assert "groq:llama-3" in keys