V9.0.1: Rainer Quality Gate — 4 P1-Fixes

F1.1: Hook-Adapter-Bridge nutzt offizielle API (Modul-Level _active_adapter
      statt privater ctx._adapter/ctx._platforms)
F1.2: Hook-Registrierung in connect()-Lifecycle-Phase (nicht register())
      — Adapter ist dann vollständig initialisiert
F1.3: asyncio.Lock lazy-created (Python 3.14 forward-compat)
F2.1: 3 neue 429-Tests (kein Crash, Retry bei nächstem Hook, Telemetrie)

Tests: 39 passed, 99 skipped (V8.x), 0 failed
This commit is contained in:
Bernd (Hermes)
2026-08-03 21:36:37 +02:00
parent 0322f07de3
commit 5fb0d08d4a
4 changed files with 177 additions and 25 deletions
+16 -2
View File
@@ -87,7 +87,10 @@ class PresenceEngine:
# ── State-Maschine ────────────────────────────────────────────────
self._status: str = "ONLINE" # ONLINE | BUSY
self._transition_lock = asyncio.Lock()
# F1.3: asyncio.Lock lazy-created to avoid Python 3.14 deprecation.
# Creating asyncio.Lock() outside a running event loop is deprecated
# in 3.14+. We defer creation to first use (always inside async context).
self._transition_lock: Optional[asyncio.Lock] = None
# ── away-grace: EIN async Task für ONLINE-Transition ──────────────
self._away_grace_task: Optional[asyncio.Future] = None
@@ -207,6 +210,17 @@ class PresenceEngine:
# ── Interne State-Maschine ───────────────────────────────────────────
def _get_lock(self) -> asyncio.Lock:
"""Lazy-create asyncio.Lock (F1.3: Python 3.14 forward-compat).
Creating asyncio.Lock() outside a running event loop is deprecated
in Python 3.14+. This method defers creation to first use, which
always happens inside an async context (Hook-Callback).
"""
if self._transition_lock is None:
self._transition_lock = asyncio.Lock()
return self._transition_lock
async def _apply_counter_delta(self, counter_name: str, delta: int) -> None:
"""Atomar: Counter mutieren, Summe prüfen, ggf. Transition auslösen.
@@ -217,7 +231,7 @@ class PresenceEngine:
if not self._enabled:
return
async with self._transition_lock:
async with self._get_lock():
# ── Counter mutieren ─────────────────────────────────────────
old_value = self.counters[counter_name]
new_value = old_value + delta
@@ -629,6 +629,86 @@ class TestShutdown:
loop.close()
# ═══════════════════════════════════════════════════════════════════════════
# F2.1: 429-Tests (Rainer Quality Gate)
# ═══════════════════════════════════════════════════════════════════════════
class TestHttp429Handling:
"""HTTP 429 von users.setStatus — kein Crash, Status bleibt, Telemetrie."""
def test_429_no_crash_status_unchanged(self):
"""429-Antwort → kein Crash, interner Status bleibt unverändert."""
rest = _make_mock_rest()
# Simuliere 429: _request wirft Exception mit "429" im Text
rest._request.side_effect = Exception("HTTP 429 Too Many Requests")
engine, loop = _make_engine(rest=rest, away_grace_s=0.05)
async def run():
# Setze Status auf BUSY
await engine.on_pre_gateway_dispatch()
assert engine.get_status() == "BUSY"
# Warte auf Transition (REST-Call schlägt fehl mit 429)
await asyncio.sleep(0.2)
# Status bleibt BUSY — Counter wurde nicht zurückgesetzt
return engine.get_status(), engine.get_counters()
status, counters = loop.run_until_complete(run())
# Status bleibt BUSY weil Counter > 0 und REST-Call fehlschlug
assert status == "BUSY"
assert counters["main_agent"] == 1
loop.close()
def test_429_retry_on_next_hook(self):
"""Nach 429 wird bei nächstem Hook-Ereignis erneut gesendet."""
rest = _make_mock_rest()
# Erster Call: 429, zweiter Call: Erfolg
call_count = [0]
def _side_effect(*args, **kwargs):
call_count[0] += 1
if call_count[0] == 1:
raise Exception("HTTP 429 Too Many Requests")
return {"success": True}
rest._request.side_effect = _side_effect
engine, loop = _make_engine(rest=rest, away_grace_s=0.05)
async def run():
# Erster Hook: BUSY → REST-Call schlägt fehl (429)
await engine.on_pre_gateway_dispatch()
await asyncio.sleep(0.15)
# Zweiter Hook: post_llm_call → Counter=0 → ONLINE-Transition
# Der REST-Call sollte jetzt erfolgreich sein
await engine.on_post_llm_call()
await asyncio.sleep(0.2)
return engine.get_status(), call_count[0]
status, calls = loop.run_until_complete(run())
# Nach post_llm_call ist Counter=0, away-grace läuft durch,
# ONLINE-Transition wird gesendet (zweiter REST-Call, erfolgreich)
assert status == "ONLINE"
assert calls >= 2 # Mindestens 2 REST-Calls (429 + success)
loop.close()
def test_429_telemetry_counted(self):
"""Telemetrie zählt 429 korrekt."""
rest = _make_mock_rest()
rest._request.side_effect = Exception("HTTP 429 Too Many Requests")
engine, loop = _make_engine(rest=rest, away_grace_s=0.05)
async def run():
await engine.on_pre_gateway_dispatch()
await asyncio.sleep(0.2)
return engine.get_telemetry()
telemetry = loop.run_until_complete(run())
assert telemetry["rest_429"] >= 1
assert telemetry["rest_calls"] >= 1
# Kein success weil 429
assert telemetry["rest_success"] == 0
loop.close()
# ═══════════════════════════════════════════════════════════════════════════
# V8.x Tests — SKIPPED (V8.x removed in V9)
# ═══════════════════════════════════════════════════════════════════════════