feat(F100): Self-Check Stufe 4b (Router-E2E) + Runtime-Watchdog
Zweite Verteidigungslinie gegen die 110 Router-Timeouts/7d (Andreas-Direktive 01.09.): Stufe 4b prüft beim Start, dass Modell+Endpoint wirklich antworten (Smoke-Test), der Runtime-Watchdog misst die echte Router-Pipeline über record_attempt() nach jedem Live-Call. D1 Stufe 4b (self_check.py): - _probe_llm_room_e2e(): POST /api/generate mit Mini-Prompt, Latenz <= 15s - ComponentCheckResult + llm_room_e2e_ok / llm_room_e2e_latency_ms - ready = ... AND llm_room_e2e_ok; needs_warmup schliesst 4b-Fail ein - Prompt empirisch kalibriert (qwen2.5:7b): 'Antworte ausschließlich mit dem Wort STAY' statt naivem 'genau einem Wort: STAY' (lieferte 'Ok.') D2 Runtime-Watchdog (routing_counter.py + router.py + adapter.py): - RoutingCounter.record_attempt() + success_ratio(window_s, min_samples) (NEUE Methoden, increment() unverändert — P-019, 165 Call-Sites) - record_attempt() an success/timeout/exception-Branches (router.py Z.1670+) - _start_routing_watchdog() neben _start_self_check() in connect() - 5 Konstanten in config.py (Kalibrierungs-Kommentar 'nach 7 Tagen prüfen') K3 Cooldown (watchdog_alert.py, NEU): - 24h-Cooldown + persistentes Flag in ~/.hermes/hermes-agent/data/ routing_watchdog_alert.json (last_alert_ts, atomar via tmp+rename P-101) - logger.error (KEIN RC-Post, P-088-Lehre) D3 Tests: test_f92_self_check erweitert (TestStufe4bProbe, TestComponentCheckResult4b), test_f92_routing_counter.py (Window/Min-Samples/ Boundary/GC), test_f100_watchdog.py (Trigger/Cooldown/Min-Samples/Persistenz). D4 Verifikation: echte Probe gegen localhost:11434 (qwen2.5:7b) → ok=True, latency ~850ms. 98 Tests grün (F92/F93/F100 + Stufe4-Red-Flag). KEIN Push, KEIN Deploy — wartet auf QG-Freigabe Rainer.
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
"""
|
||||
F100 — RoutingCounter-Erweiterung: record_attempt() + success_ratio().
|
||||
|
||||
Testkonzept (D3):
|
||||
1. record_attempt_window: Attempts im Fenster zählen, ältere nicht.
|
||||
2. min_samples_guard: < min_samples → None.
|
||||
3. threshold_boundary: ratio exakt an der Schwelle.
|
||||
4. window_gc: Einträge älter als window_s werden beim Lesen gefiltert.
|
||||
5. Rückwärts-Kompatibilität: increment() unverändert (P-019).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from plugin.archivar.routing_counter import RoutingCounter
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def counter():
|
||||
return RoutingCounter()
|
||||
|
||||
|
||||
class TestRecordAttemptWindow:
|
||||
"""Attempts im Fenster zählen, ältere nicht."""
|
||||
|
||||
def test_all_in_window_ratio_1(self, counter):
|
||||
for i in range(10):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=10, now=1000.0 + 10)
|
||||
assert ratio == 1.0
|
||||
|
||||
def test_old_attempts_excluded(self, counter):
|
||||
# 5 Attempts vor dem Fenster, 5 im Fenster.
|
||||
# now=1000, window=100 → cutoff=900. Alte Attempts bei 0..4 (< 900).
|
||||
for i in range(5):
|
||||
counter.record_attempt(False, timestamp=0.0 + i) # alt (vor Fenster)
|
||||
for i in range(5):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i) # im Fenster
|
||||
ratio = counter.success_ratio(window_s=100.0, min_samples=5, now=1000.0 + 5)
|
||||
assert ratio == 1.0 # nur die 5 im Fenster zählen
|
||||
|
||||
def test_mixed_ratio(self, counter):
|
||||
# 8 Erfolge + 2 Fehler = 0.8
|
||||
for i in range(8):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
for i in range(2):
|
||||
counter.record_attempt(False, timestamp=1000.0 + 8 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=10, now=1000.0 + 10)
|
||||
assert ratio == pytest.approx(0.8)
|
||||
|
||||
|
||||
class TestMinSamplesGuard:
|
||||
"""< min_samples → None (K2)."""
|
||||
|
||||
def test_three_attempts_returns_none(self, counter):
|
||||
for i in range(3):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=10, now=1000.0 + 3)
|
||||
assert ratio is None
|
||||
|
||||
def test_exactly_min_samples_returns_ratio(self, counter):
|
||||
for i in range(10):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=10, now=1000.0 + 10)
|
||||
assert ratio == 1.0
|
||||
|
||||
def test_zero_attempts_returns_none(self, counter):
|
||||
assert counter.success_ratio(window_s=3600.0, min_samples=10, now=1000.0) is None
|
||||
|
||||
|
||||
class TestThresholdBoundary:
|
||||
"""ratio exakt an der Schwelle (0.79 → trigger, 0.81 → kein trigger)."""
|
||||
|
||||
def test_ratio_079(self, counter):
|
||||
# 79 Erfolge + 21 Fehler = 0.79
|
||||
for i in range(79):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
for i in range(21):
|
||||
counter.record_attempt(False, timestamp=1000.0 + 79 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=100, now=1000.0 + 100)
|
||||
assert ratio == pytest.approx(0.79)
|
||||
|
||||
def test_ratio_081(self, counter):
|
||||
# 81 Erfolge + 19 Fehler = 0.81
|
||||
for i in range(81):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
for i in range(19):
|
||||
counter.record_attempt(False, timestamp=1000.0 + 81 + i)
|
||||
ratio = counter.success_ratio(window_s=3600.0, min_samples=100, now=1000.0 + 100)
|
||||
assert ratio == pytest.approx(0.81)
|
||||
|
||||
|
||||
class TestWindowGC:
|
||||
"""Einträge älter als window_s werden beim Lesen gefiltert (Ring-Buffer)."""
|
||||
|
||||
def test_gc_removes_old_entries(self, counter):
|
||||
for i in range(10):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
# Nach dem Lesen mit engem Fenster sind die alten Einträge weg.
|
||||
# now=2000, window=1 → cutoff=1999. Alle Attempts (1000..1009) < 1999.
|
||||
counter.success_ratio(window_s=1.0, min_samples=1, now=2000.0)
|
||||
# attempt_count mit engem Fenster → 0 (alle gefiltert).
|
||||
assert counter.attempt_count(window_s=1.0, now=2000.0) == 0
|
||||
|
||||
def test_gc_keeps_recent_entries(self, counter):
|
||||
for i in range(10):
|
||||
counter.record_attempt(True, timestamp=1000.0 + i)
|
||||
counter.success_ratio(window_s=3600.0, min_samples=1, now=1000.0 + 10)
|
||||
assert counter.attempt_count(window_s=3600.0, now=1000.0 + 10) == 10
|
||||
|
||||
|
||||
class TestBackwardCompatibility:
|
||||
"""increment() bleibt unverändert (P-019 — 165 Call-Sites)."""
|
||||
|
||||
def test_increment_still_works(self, counter):
|
||||
counter.increment("llm_room_lookup_timeout")
|
||||
counter.increment("llm_room_lookup_timeout")
|
||||
assert counter.get_counts()["llm_room_lookup_timeout"] == 2
|
||||
|
||||
def test_reset_clears_both(self, counter):
|
||||
counter.increment("foo")
|
||||
counter.record_attempt(True, timestamp=1000.0)
|
||||
counter.reset()
|
||||
assert counter.get_counts() == {}
|
||||
assert counter.attempt_count(window_s=3600.0, now=1000.0) == 0
|
||||
Reference in New Issue
Block a user