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
|
self._cancel_pending_event = threading.Event() # B6: thread-safe via Event statt bool
|
||||||
# P1-2 O-2: Async-await bridge
|
# P1-2 O-2: Async-await bridge
|
||||||
self._loop: Optional[asyncio.AbstractEventLoop] = None
|
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"
|
# Phase 5: pulse_target — "away" (default) oder "busy"
|
||||||
# "away": pulsiert online↔away (Subagent läuft, Hauptagent idle)
|
# "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._mode_lock = threading.Lock() # Mode-Sentinel (Risiko #2)
|
||||||
self._tick_counter: int = 0 # für Message-only-every-2-ticks
|
self._tick_counter: int = 0 # für Message-only-every-2-ticks
|
||||||
|
|
||||||
# V8.8: Mode-abhängige Pulse-Intervalle
|
# V8.9: Mode-abhängige Pulse-Intervalle (Timeout-Hardening)
|
||||||
# away-pulse: 3.0s (weniger aggressiv, vermeidet 429)
|
# away-pulse: 6.0s (seltener, vermeidet 429)
|
||||||
# busy-pulse: 1.5s (schnelle Reaktion auf busy-Status)
|
# busy-pulse: 4.0s (schnelle Reaktion, aber nicht zu aggressiv)
|
||||||
self._away_pulse_interval: float = 3.0
|
self._away_pulse_interval: float = 6.0
|
||||||
self._busy_pulse_interval: float = 1.5
|
self._busy_pulse_interval: float = 4.0
|
||||||
|
|
||||||
# P1: Circuit-Breaker — verhindert 429-Endlosschleife im Gateway
|
# P1: Circuit-Breaker — verhindert 429-Endlosschleife im Gateway
|
||||||
# States: CLOSED (normal) → OPEN (nach N×429: Pause) → HALF_OPEN (Test-Ping)
|
# States: CLOSED (normal) → OPEN (nach N×429: Pause) → HALF_OPEN (Test-Ping)
|
||||||
self._breaker_state: str = "CLOSED"
|
self._breaker_state: str = "CLOSED"
|
||||||
self._consecutive_429s: int = 0
|
self._consecutive_429s: int = 0
|
||||||
self._breaker_opened_at: float = 0.0
|
self._breaker_opened_at: float = 0.0
|
||||||
self._breaker_cooldown_s: float = 30.0
|
self._breaker_cooldown_s: float = 45.0
|
||||||
self._breaker_429_threshold: int = 1 # V8.8: 1 statt 3 — sofort nach erstem 429
|
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._base_pulse_interval: float = self._pulse_interval
|
||||||
self._backoff_level: int = 0
|
self._backoff_level: int = 0
|
||||||
# Adaptive Backoff: pulse_interval wird bei OPEN eskaliert
|
# 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 ───────────────────────────────────────────────────────
|
# ── Public API ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
@@ -908,9 +908,9 @@ class PulseController:
|
|||||||
def set_pulse_target(self, target: str) -> bool:
|
def set_pulse_target(self, target: str) -> bool:
|
||||||
"""Phase 5: Set the pulse target mode with Mode-Sentinel.
|
"""Phase 5: Set the pulse target mode with Mode-Sentinel.
|
||||||
|
|
||||||
V8.8: Mode-abhängige Pulse-Intervalle:
|
V8.9: Mode-abhängige Pulse-Intervalle (Timeout-Hardening):
|
||||||
- "away": 3.0s (weniger aggressiv, vermeidet 429)
|
- "away": 6.0s (seltener, vermeidet 429)
|
||||||
- "busy": 1.5s (schnelle Reaktion auf busy-Status)
|
- "busy": 4.0s (schnelle Reaktion, aber nicht zu aggressiv)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
target: "away" (pulse online↔away) or "busy" (pulse message while busy).
|
target: "away" (pulse online↔away) or "busy" (pulse message while busy).
|
||||||
|
|||||||
@@ -2347,7 +2347,7 @@ class TestCircuitBreakerStateTransitions:
|
|||||||
assert pc.get_consecutive_429s() == 0
|
assert pc.get_consecutive_429s() == 0
|
||||||
|
|
||||||
def test_one_429_opens_breaker(self):
|
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
|
from plugin.archivar.presence_engine import PulseController
|
||||||
|
|
||||||
mock_ddp = MagicMock()
|
mock_ddp = MagicMock()
|
||||||
@@ -2360,11 +2360,12 @@ class TestCircuitBreakerStateTransitions:
|
|||||||
|
|
||||||
base_interval = pc.get_effective_pulse_interval()
|
base_interval = pc.get_effective_pulse_interval()
|
||||||
|
|
||||||
# Tick 1: 429 #1 → OPEN (threshold=1)
|
# threshold×429 → OPEN
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
assert pc.get_breaker_state() == "OPEN"
|
||||||
assert pc.get_consecutive_429s() == 1
|
assert pc.get_consecutive_429s() == pc._breaker_429_threshold
|
||||||
# pulse_interval sollte auf backoff_level=0 → 5.0s gestiegen sein
|
# pulse_interval sollte auf backoff_level=0 → 10.0s gestiegen sein
|
||||||
assert pc.get_effective_pulse_interval() > base_interval
|
assert pc.get_effective_pulse_interval() > base_interval
|
||||||
|
|
||||||
def test_breaker_blocks_ticks_when_open(self):
|
def test_breaker_blocks_ticks_when_open(self):
|
||||||
@@ -2378,7 +2379,8 @@ class TestCircuitBreakerStateTransitions:
|
|||||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||||
pc.bind_ddp(mock_ddp)
|
pc.bind_ddp(mock_ddp)
|
||||||
|
|
||||||
# 1×429 → OPEN (threshold=1)
|
# threshold×429 → OPEN
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
assert pc.get_breaker_state() == "OPEN"
|
||||||
|
|
||||||
@@ -2397,12 +2399,12 @@ class TestCircuitBreakerHalfOpen:
|
|||||||
"""HALF_OPEN Test-Ping success → CLOSED, pulse_interval reset."""
|
"""HALF_OPEN Test-Ping success → CLOSED, pulse_interval reset."""
|
||||||
from plugin.archivar.presence_engine import PulseController
|
from plugin.archivar.presence_engine import PulseController
|
||||||
|
|
||||||
# DDP wirft 1×429, dann success (threshold=1)
|
# DDP wirft threshold×429, dann success
|
||||||
call_count = [0]
|
call_count = [0]
|
||||||
|
|
||||||
def ddp_side_effect(status):
|
def ddp_side_effect(status):
|
||||||
call_count[0] += 1
|
call_count[0] += 1
|
||||||
if call_count[0] <= 1:
|
if call_count[0] <= pc._breaker_429_threshold:
|
||||||
raise Exception("HTTP 429 Too Many Requests")
|
raise Exception("HTTP 429 Too Many Requests")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -2413,7 +2415,8 @@ class TestCircuitBreakerHalfOpen:
|
|||||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||||
pc.bind_ddp(mock_ddp)
|
pc.bind_ddp(mock_ddp)
|
||||||
|
|
||||||
# 1×429 → OPEN (threshold=1)
|
# threshold×429 → OPEN
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
assert pc.get_breaker_state() == "OPEN"
|
||||||
|
|
||||||
@@ -2439,7 +2442,8 @@ class TestCircuitBreakerHalfOpen:
|
|||||||
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
pc = PulseController(pulse_interval=0.05, _clamp_pulse=False)
|
||||||
pc.bind_ddp(mock_ddp)
|
pc.bind_ddp(mock_ddp)
|
||||||
|
|
||||||
# 1×429 → OPEN (threshold=1, backoff_level=0, pulse_interval=5.0)
|
# threshold×429 → OPEN (backoff_level=0, pulse_interval=10.0)
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
assert pc.get_breaker_state() == "OPEN"
|
||||||
first_open_interval = pc.get_effective_pulse_interval()
|
first_open_interval = pc.get_effective_pulse_interval()
|
||||||
@@ -2447,7 +2451,7 @@ class TestCircuitBreakerHalfOpen:
|
|||||||
# Simuliere Cooldown-Ablauf
|
# Simuliere Cooldown-Ablauf
|
||||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
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()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
assert pc.get_breaker_state() == "OPEN"
|
||||||
assert pc.get_effective_pulse_interval() > first_open_interval
|
assert pc.get_effective_pulse_interval() > first_open_interval
|
||||||
@@ -2457,7 +2461,7 @@ class TestCircuitBreakerSustained429:
|
|||||||
"""P1: sustained-429 — adaptives pulse_interval steigt unter Last."""
|
"""P1: sustained-429 — adaptives pulse_interval steigt unter Last."""
|
||||||
|
|
||||||
def test_sustained_429_escalates_pulse_interval(self):
|
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
|
from plugin.archivar.presence_engine import PulseController
|
||||||
|
|
||||||
mock_ddp = MagicMock()
|
mock_ddp = MagicMock()
|
||||||
@@ -2469,11 +2473,12 @@ class TestCircuitBreakerSustained429:
|
|||||||
|
|
||||||
intervals_seen = [pc.get_effective_pulse_interval()]
|
intervals_seen = [pc.get_effective_pulse_interval()]
|
||||||
|
|
||||||
# Zyklus 1: 1×429 → OPEN (threshold=1, backoff 0: 5.0s)
|
# Zyklus 1: threshold×429 → OPEN (backoff 0: 10.0s)
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
intervals_seen.append(pc.get_effective_pulse_interval())
|
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._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
intervals_seen.append(pc.get_effective_pulse_interval())
|
intervals_seen.append(pc.get_effective_pulse_interval())
|
||||||
@@ -2507,7 +2512,8 @@ class TestCircuitBreakerSustained429:
|
|||||||
|
|
||||||
# 5 Zyklen OPEN→HALF_OPEN→OPEN (mehr als backoff_intervals Länge)
|
# 5 Zyklen OPEN→HALF_OPEN→OPEN (mehr als backoff_intervals Länge)
|
||||||
for cycle in range(5):
|
for cycle in range(5):
|
||||||
# 1×429 → OPEN (threshold=1)
|
# threshold×429 → OPEN
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
# Cooldown ablaufen → HALF_OPEN → 429 → OPEN
|
# Cooldown ablaufen → HALF_OPEN → 429 → OPEN
|
||||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||||
@@ -2589,7 +2595,8 @@ class TestCircuitBreakerDdpErrorClassification:
|
|||||||
pc.bind_ddp(mock_ddp)
|
pc.bind_ddp(mock_ddp)
|
||||||
pc.bind_rest(mock_rest)
|
pc.bind_rest(mock_rest)
|
||||||
|
|
||||||
# 1×429 DDP → OPEN (threshold=1), REST wurde NIE aufgerufen
|
# threshold×429 DDP → OPEN, REST wurde NIE aufgerufen
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
|
|
||||||
# REST wurde NICHT aufgerufen (429 blockiert Fallback)
|
# REST wurde NICHT aufgerufen (429 blockiert Fallback)
|
||||||
@@ -2626,7 +2633,7 @@ class TestCircuitBreakerRecordSuccess:
|
|||||||
|
|
||||||
def ddp_side_effect(status):
|
def ddp_side_effect(status):
|
||||||
call_count[0] += 1
|
call_count[0] += 1
|
||||||
if call_count[0] <= 1:
|
if call_count[0] <= pc._breaker_429_threshold:
|
||||||
raise Exception("HTTP 429 Too Many Requests")
|
raise Exception("HTTP 429 Too Many Requests")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -2637,10 +2644,11 @@ class TestCircuitBreakerRecordSuccess:
|
|||||||
pc = PulseController(pulse_interval=1.5, _clamp_pulse=False)
|
pc = PulseController(pulse_interval=1.5, _clamp_pulse=False)
|
||||||
pc.bind_ddp(mock_ddp)
|
pc.bind_ddp(mock_ddp)
|
||||||
|
|
||||||
# 1×429 → OPEN (threshold=1, pulse_interval=5.0)
|
# threshold×429 → OPEN (backoff_level=0, pulse_interval=10.0)
|
||||||
|
for _ in range(pc._breaker_429_threshold):
|
||||||
pc._pulse_tick()
|
pc._pulse_tick()
|
||||||
assert pc.get_breaker_state() == "OPEN"
|
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
|
# Cooldown ablaufen → HALF_OPEN → success → CLOSED
|
||||||
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
pc._breaker_opened_at = time.monotonic() - pc._breaker_cooldown_s - 1.0
|
||||||
|
|||||||
Reference in New Issue
Block a user