fix(presence): P-090+P-091 429 spiral — backoff N=3 successes + adaptive pulse interval

This commit is contained in:
Bernd (Hermes)
2026-08-22 07:27:18 +02:00
parent ddd3fb4027
commit b6f532a4a8
3 changed files with 261 additions and 15 deletions
+68 -15
View File
@@ -140,6 +140,10 @@ class _Mediator:
REST_BACKOFF_429_S: float = 5.0
COOLDOWN_RECONCILE_DELAY_S: float = 0.1
BACKOFF_MAX_S: float = 60.0 # P-036: Cap für exponentielles Backoff
# P-090: Circuit-Breaker HALF_OPEN — Backoff-Reset erst nach N konsekutiven Erfolgen.
REST_SUCCESS_THRESHOLD: int = 3
# P-091: Adaptive Pulse-Intervall-Eskalation bei 429-Streak.
PULSE_ESCALATION_STEPS: list = [2.5, 5.0, 10.0, 30.0, 60.0]
def __init__(self, rest_sender, away_grace_s: float = 2.0, ddp_sender=None):
"""Initialisiere den Mediator.
@@ -177,6 +181,10 @@ class _Mediator:
# ── P-036: Exponentielles Backoff ─────────────────────────────────
self._consecutive_429s: int = 0
# P-090: Konsekutive Erfolge (Circuit-Breaker HALF_OPEN).
self._consecutive_successes: int = 0
# P-091: Adaptives Pulse-Intervall (Start bei 2.5s).
self._adaptive_pulse_interval: float = 2.5
# ── P-036: Counter-Drift-Korrektur ────────────────────────────────
self._drift_correction_timer: Optional[threading.Timer] = None
@@ -531,9 +539,14 @@ class _Mediator:
Formel: cooldown = min(5 * 2^n + random(0,1), 60)
Sequence: 5s -> 10s -> 20s -> 40s -> 60s (cap).
P-090: Reset des Success-Streaks bei 429 (Circuit-Breaker).
P-091: Adaptive Pulse-Intervall-Eskalation bei 429-Streak.
Plant automatisch Reconciliation nach Cooldown-Ende.
"""
with self._mediator_lock:
# P-090: 429 bricht den Success-Streak ab.
self._consecutive_successes = 0
self._consecutive_429s += 1
n = self._consecutive_429s
backoff = min(
@@ -546,18 +559,55 @@ class _Mediator:
"[presence-v10] REST-Cooldown gesetzt bis %.1f (%.1fs, n=%d, backoff=exp)",
self._rest_cooldown_until, backoff, n,
)
# P-091: Adaptive Pulse-Intervall-Eskalation.
escalation_idx = min(
self._consecutive_429s // 3,
len(self.PULSE_ESCALATION_STEPS) - 1,
)
new_interval = self.PULSE_ESCALATION_STEPS[escalation_idx]
if new_interval != self._adaptive_pulse_interval:
logger.warning(
"[presence-v10] P-091: Pulse-Intervall eskaliert: "
"%.1fs → %.1fs (429-Streak=%d)",
self._adaptive_pulse_interval, new_interval,
self._consecutive_429s,
)
self._adaptive_pulse_interval = new_interval
# Plane Reconciliation nach Cooldown-Ende
self._schedule_reconcile_locked()
def reset_backoff(self) -> None:
"""Reset exponentielles Backoff nach erfolgreichem REST-Call (P-036 Fix 1)."""
def record_success(self) -> None:
"""Record a successful REST/DDP call (P-090 + P-091).
Circuit-Breaker HALF_OPEN: Backoff-Reset erst nach N=3 konsekutiven
Erfolgen. Ein einzelner Erfolg zwischen 429s ist kein Gesundungs-Signal.
P-091: Langsame Rück-Eskalation des Pulse-Intervalls (1 Stufe pro
3 Erfolge) bei Gesundung.
"""
with self._mediator_lock:
if self._consecutive_429s > 0:
logger.info(
"[presence-v10] Backoff reset nach %d aufeinanderfolgenden 429s",
self._consecutive_429s,
)
self._consecutive_429s = 0
self._consecutive_successes += 1
if self._consecutive_successes >= self.REST_SUCCESS_THRESHOLD:
if self._consecutive_429s > 0:
logger.info(
"[presence-v10] Backoff reset nach %d aufeinanderfolgenden 429s "
"(erforderte %d konsekutive Erfolge)",
self._consecutive_429s, self._consecutive_successes,
)
self._consecutive_429s = 0
self._consecutive_successes = 0
self._rest_cooldown_until = 0.0
# P-091: Langsame Rück-Eskalation (1 Stufe pro 3 Erfolge).
if self._adaptive_pulse_interval > 2.5:
self._adaptive_pulse_interval = max(
2.5, self._adaptive_pulse_interval / 2
)
logger.info(
"[presence-v10] P-091: Pulse-Intervall deeskaliert: "
"%.1fs (Gesundung)",
self._adaptive_pulse_interval,
)
# Bei < N Erfolgen: Counter hochzählen, aber NICHT resetten.
def shutdown(self) -> None:
"""Clean shutdown: stoppe Puls-Timer, away-grace, Reconciliation, Drift-Korrektur, Modal-Puls, Heartbeat."""
@@ -778,12 +828,15 @@ class _Mediator:
return # Läuft bereits
self._pulse_timer = threading.Timer(
self.PULSE_INTERVAL_S,
self._adaptive_pulse_interval,
self._on_pulse_tick,
)
self._pulse_timer.daemon = True
self._pulse_timer.start()
logger.debug("[presence-v10] Puls-Timer gestartet (%.1fs)", self.PULSE_INTERVAL_S)
logger.debug(
"[presence-v10] Puls-Timer gestartet (%.1fs)",
self._adaptive_pulse_interval,
)
def _stop_pulse_locked(self) -> None:
"""Stoppe Puls-Timer.
@@ -855,7 +908,7 @@ class _Mediator:
return
self._pulse_timer = threading.Timer(
self.PULSE_INTERVAL_S,
self._adaptive_pulse_interval,
self._on_pulse_tick,
)
self._pulse_timer.daemon = True
@@ -2494,8 +2547,8 @@ class PresenceEngine:
)
self._telemetry_rest_success += 1
# P-036 Fix 1: Reset exponentielles Backoff bei Erfolg
self._mediator.reset_backoff()
# P-090: Record success (Circuit-Breaker HALF_OPEN, N=3).
self._mediator.record_success()
# P-050: Modal-Puls-Erfolg signalisieren
if self._mediator._clarify_modal:
self._mediator._on_modal_pulse_success()
@@ -2545,8 +2598,8 @@ class PresenceEngine:
data=data,
)
self._telemetry_rest_success += 1
# P-036 Fix 1: Reset exponentielles Backoff bei Erfolg
self._mediator.reset_backoff()
# P-090: Record success (Circuit-Breaker HALF_OPEN, N=3).
self._mediator.record_success()
# P-050: Modal-Puls-Erfolg signalisieren
if self._mediator._clarify_modal:
self._mediator._on_modal_pulse_success()