fix(presence): Pulse-Engine Timeout-Hardening (Rainer Architects Review)
6 hardcoded Werte in PulseController geaendert: - _await_timeout: 1.0 → 4.0s (DDP setDefaultStatus braucht mehr Zeit) - _away_pulse_interval: 3.0 → 6.0s (seltener, vermeidet 429) - _busy_pulse_interval: 1.5 → 4.0s (nicht zu aggressiv) - _breaker_429_threshold: 1 → 3 (nicht sofort nach erstem 429 oeffnen) - _backoff_intervals: (5,15,60) → (10,30,60) (sanftere Eskalation) - _breaker_cooldown_s: 30.0 → 45.0s (laengere Erholungsphase) Tests: threshold-abhaengige Tests dynamisch auf _breaker_429_threshold umgestellt. 103/106 passed (3 pre-existing pgrep-mock Fehler). ENV-Check: ROCKETCHAT_PRESENCE_PULSE_INTERVAL setzt nur initialen _pulse_interval, set_pulse_target ueberschreibt mit mode-spezifischen Werten — neue Defaults greifen korrekt.
This commit is contained in:
@@ -844,7 +844,7 @@ class PulseController:
|
||||
self._cancel_pending_event = threading.Event() # B6: thread-safe via Event statt bool
|
||||
# P1-2 O-2: Async-await bridge
|
||||
self._loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
self._await_timeout: float = 1.0 # V8.8: 1.0s statt 2.0s — schneller fail bei 429
|
||||
self._await_timeout: float = 4.0 # V8.9: 4.0s — DDP setDefaultStatus braucht mehr Zeit
|
||||
|
||||
# Phase 5: pulse_target — "away" (default) oder "busy"
|
||||
# "away": pulsiert online↔away (Subagent läuft, Hauptagent idle)
|
||||
@@ -853,23 +853,23 @@ class PulseController:
|
||||
self._mode_lock = threading.Lock() # Mode-Sentinel (Risiko #2)
|
||||
self._tick_counter: int = 0 # für Message-only-every-2-ticks
|
||||
|
||||
# V8.8: Mode-abhängige Pulse-Intervalle
|
||||
# away-pulse: 3.0s (weniger aggressiv, vermeidet 429)
|
||||
# busy-pulse: 1.5s (schnelle Reaktion auf busy-Status)
|
||||
self._away_pulse_interval: float = 3.0
|
||||
self._busy_pulse_interval: float = 1.5
|
||||
# V8.9: Mode-abhängige Pulse-Intervalle (Timeout-Hardening)
|
||||
# away-pulse: 6.0s (seltener, vermeidet 429)
|
||||
# busy-pulse: 4.0s (schnelle Reaktion, aber nicht zu aggressiv)
|
||||
self._away_pulse_interval: float = 6.0
|
||||
self._busy_pulse_interval: float = 4.0
|
||||
|
||||
# P1: Circuit-Breaker — verhindert 429-Endlosschleife im Gateway
|
||||
# States: CLOSED (normal) → OPEN (nach N×429: Pause) → HALF_OPEN (Test-Ping)
|
||||
self._breaker_state: str = "CLOSED"
|
||||
self._consecutive_429s: int = 0
|
||||
self._breaker_opened_at: float = 0.0
|
||||
self._breaker_cooldown_s: float = 30.0
|
||||
self._breaker_429_threshold: int = 1 # V8.8: 1 statt 3 — sofort nach erstem 429
|
||||
self._breaker_cooldown_s: float = 45.0
|
||||
self._breaker_429_threshold: int = 3 # V8.9: 3 statt 1 — nicht sofort nach erstem 429 öffnen
|
||||
self._base_pulse_interval: float = self._pulse_interval
|
||||
self._backoff_level: int = 0
|
||||
# Adaptive Backoff: pulse_interval wird bei OPEN eskaliert
|
||||
self._backoff_intervals: tuple = (5.0, 15.0, 60.0)
|
||||
self._backoff_intervals: tuple = (10.0, 30.0, 60.0)
|
||||
|
||||
# ── Public API ───────────────────────────────────────────────────────
|
||||
|
||||
@@ -908,9 +908,9 @@ class PulseController:
|
||||
def set_pulse_target(self, target: str) -> bool:
|
||||
"""Phase 5: Set the pulse target mode with Mode-Sentinel.
|
||||
|
||||
V8.8: Mode-abhängige Pulse-Intervalle:
|
||||
- "away": 3.0s (weniger aggressiv, vermeidet 429)
|
||||
- "busy": 1.5s (schnelle Reaktion auf busy-Status)
|
||||
V8.9: Mode-abhängige Pulse-Intervalle (Timeout-Hardening):
|
||||
- "away": 6.0s (seltener, vermeidet 429)
|
||||
- "busy": 4.0s (schnelle Reaktion, aber nicht zu aggressiv)
|
||||
|
||||
Args:
|
||||
target: "away" (pulse online↔away) or "busy" (pulse message while busy).
|
||||
|
||||
@@ -2347,7 +2347,7 @@ class TestCircuitBreakerStateTransitions:
|
||||
assert pc.get_consecutive_429s() == 0
|
||||
|
||||
def test_one_429_opens_breaker(self):
|
||||
"""V8.8: 1×429 → CLOSED→OPEN sofort (threshold=1)."""
|
||||
"""V8.9: threshold×429 → CLOSED→OPEN (threshold=3)."""
|
||||
from plugin.archivar.presence_engine import PulseController
|
||||
|
||||
mock_ddp = MagicMock()
|
||||
@@ -2360,11 +2360,12 @@ class TestCircuitBreakerStateTransitions:
|
||||
|
||||
base_interval = pc.get_effective_pulse_interval()
|
||||
|
||||
# Tick 1: 429 #1 → OPEN (threshold=1)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
assert pc.get_consecutive_429s() == 1
|
||||
# pulse_interval sollte auf backoff_level=0 → 5.0s gestiegen sein
|
||||
assert pc.get_consecutive_429s() == pc._breaker_429_threshold
|
||||
# pulse_interval sollte auf backoff_level=0 → 10.0s gestiegen sein
|
||||
assert pc.get_effective_pulse_interval() > base_interval
|
||||
|
||||
def test_breaker_blocks_ticks_when_open(self):
|
||||
@@ -2378,8 +2379,9 @@ class TestCircuitBreakerStateTransitions:
|
||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||
pc.bind_ddp(mock_ddp)
|
||||
|
||||
# 1×429 → OPEN (threshold=1)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
|
||||
# Weitere Ticks werden geblockt (kein DDP-Call mehr)
|
||||
@@ -2397,12 +2399,12 @@ class TestCircuitBreakerHalfOpen:
|
||||
"""HALF_OPEN Test-Ping success → CLOSED, pulse_interval reset."""
|
||||
from plugin.archivar.presence_engine import PulseController
|
||||
|
||||
# DDP wirft 1×429, dann success (threshold=1)
|
||||
# DDP wirft threshold×429, dann success
|
||||
call_count = [0]
|
||||
|
||||
def ddp_side_effect(status):
|
||||
call_count[0] += 1
|
||||
if call_count[0] <= 1:
|
||||
if call_count[0] <= pc._breaker_429_threshold:
|
||||
raise Exception("HTTP 429 Too Many Requests")
|
||||
return True
|
||||
|
||||
@@ -2413,8 +2415,9 @@ class TestCircuitBreakerHalfOpen:
|
||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||
pc.bind_ddp(mock_ddp)
|
||||
|
||||
# 1×429 → OPEN (threshold=1)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
|
||||
# Simuliere Cooldown-Ablauf: setze _breaker_opened_at in die Vergangenheit
|
||||
@@ -2439,15 +2442,16 @@ class TestCircuitBreakerHalfOpen:
|
||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||
pc.bind_ddp(mock_ddp)
|
||||
|
||||
# 1×429 → OPEN (threshold=1, backoff_level=0, pulse_interval=5.0)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN (backoff_level=0, pulse_interval=10.0)
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
first_open_interval = pc.get_effective_pulse_interval()
|
||||
|
||||
# Simuliere Cooldown-Ablauf
|
||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||
|
||||
# HALF_OPEN Test-Ping → 429 → OPEN again, backoff_level=1 → 15.0s
|
||||
# HALF_OPEN Test-Ping → 429 → OPEN again, backoff_level=1 → 30.0s
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
assert pc.get_effective_pulse_interval() > first_open_interval
|
||||
@@ -2457,7 +2461,7 @@ class TestCircuitBreakerSustained429:
|
||||
"""P1: sustained-429 — adaptives pulse_interval steigt unter Last."""
|
||||
|
||||
def test_sustained_429_escalates_pulse_interval(self):
|
||||
"""Anhaltende 429 → pulse_interval: 1.5s → 5s → 15s → 60s."""
|
||||
"""Anhaltende 429 → pulse_interval: 1.5s → 10s → 30s → 60s."""
|
||||
from plugin.archivar.presence_engine import PulseController
|
||||
|
||||
mock_ddp = MagicMock()
|
||||
@@ -2469,11 +2473,12 @@ class TestCircuitBreakerSustained429:
|
||||
|
||||
intervals_seen = [pc.get_effective_pulse_interval()]
|
||||
|
||||
# Zyklus 1: 1×429 → OPEN (threshold=1, backoff 0: 5.0s)
|
||||
pc._pulse_tick()
|
||||
# Zyklus 1: threshold×429 → OPEN (backoff 0: 10.0s)
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
intervals_seen.append(pc.get_effective_pulse_interval())
|
||||
|
||||
# Cooldown ablaufen lassen → HALF_OPEN → 429 → OPEN (backoff 1: 15.0s)
|
||||
# Cooldown ablaufen lassen → HALF_OPEN → 429 → OPEN (backoff 1: 30.0s)
|
||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||
pc._pulse_tick()
|
||||
intervals_seen.append(pc.get_effective_pulse_interval())
|
||||
@@ -2507,8 +2512,9 @@ class TestCircuitBreakerSustained429:
|
||||
|
||||
# 5 Zyklen OPEN→HALF_OPEN→OPEN (mehr als backoff_intervals Länge)
|
||||
for cycle in range(5):
|
||||
# 1×429 → OPEN (threshold=1)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
# Cooldown ablaufen → HALF_OPEN → 429 → OPEN
|
||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||
pc._pulse_tick()
|
||||
@@ -2589,8 +2595,9 @@ class TestCircuitBreakerDdpErrorClassification:
|
||||
pc.bind_ddp(mock_ddp)
|
||||
pc.bind_rest(mock_rest)
|
||||
|
||||
# 1×429 DDP → OPEN (threshold=1), REST wurde NIE aufgerufen
|
||||
pc._pulse_tick()
|
||||
# threshold×429 DDP → OPEN, REST wurde NIE aufgerufen
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
|
||||
# REST wurde NICHT aufgerufen (429 blockiert Fallback)
|
||||
mock_rest._request.assert_not_called()
|
||||
@@ -2626,7 +2633,7 @@ class TestCircuitBreakerRecordSuccess:
|
||||
|
||||
def ddp_side_effect(status):
|
||||
call_count[0] += 1
|
||||
if call_count[0] <= 1:
|
||||
if call_count[0] <= pc._breaker_429_threshold:
|
||||
raise Exception("HTTP 429 Too Many Requests")
|
||||
return True
|
||||
|
||||
@@ -2637,10 +2644,11 @@ class TestCircuitBreakerRecordSuccess:
|
||||
pc = PulseController(pulse_interval=1.5, _clamp_pulse=False)
|
||||
pc.bind_ddp(mock_ddp)
|
||||
|
||||
# 1×429 → OPEN (threshold=1, pulse_interval=5.0)
|
||||
pc._pulse_tick()
|
||||
# threshold×429 → OPEN (backoff_level=0, pulse_interval=10.0)
|
||||
for _ in range(pc._breaker_429_threshold):
|
||||
pc._pulse_tick()
|
||||
assert pc.get_breaker_state() == "OPEN"
|
||||
assert pc.get_effective_pulse_interval() == 5.0
|
||||
assert pc.get_effective_pulse_interval() == 10.0
|
||||
|
||||
# Cooldown ablaufen → HALF_OPEN → success → CLOSED
|
||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||
|
||||
Reference in New Issue
Block a user