fix: PulseController _pulse_tick() feuert nicht — debug logging + timer fix

Root Cause: on_agent_busy() rief pause() auf, aber resume() wurde
niemals in der Produktion aufgerufen. Sobald die erste Nachricht
eintraf, blieb der Pulse-Loop im _paused-Zustand hängen.

Zusätzlich rief _set_presence() cancel_pending() bei JEDEM Status
inklusive 'online' auf — der Pulse-eigene Zielstatus wurde damit
ständig weggecancelt.

Fixes:
- decrement_active_tasks(): resume() aufrufen wenn count==0
- _set_presence(): cancel_pending() nur für non-online Status
- Alle skip-Pfade in _pulse_tick() loggen jetzt auf INFO-Level
- _pulse_loop: Iterations-Logging + PAUSED-Warnung alle 50 Zyklen
- start(): State-Dump vor Start (running, ddp, phase, paused)
- pause/resume: logger.debug → logger.info
This commit is contained in:
Bernd (Hermes)
2026-07-23 19:35:44 +02:00
parent 7657cd3665
commit 6b16381818
+60 -7
View File
@@ -186,7 +186,14 @@ class PresenceEngine:
with self._active_tasks_lock: with self._active_tasks_lock:
if self._active_tasks > 0: if self._active_tasks > 0:
self._active_tasks -= 1 self._active_tasks -= 1
return self._active_tasks count = self._active_tasks
# FIX: Resume pulse when all active tasks complete.
# on_agent_busy() pauses the pulse, but resume() was never called
# in production — the pulse stayed paused forever.
if count == 0:
self._pulse_controller.resume()
logger.info("[presence] active_tasks=0 → pulse resumed")
return count
def set_onboarding_active(self, active: bool) -> None: def set_onboarding_active(self, active: bool) -> None:
"""Set the onboarding sentinel.""" """Set the onboarding sentinel."""
@@ -250,7 +257,10 @@ class PresenceEngine:
if not self._enabled: if not self._enabled:
return return
# EC-5: Cancel pending pulse when higher-priority status fires # EC-5: Cancel pending pulse when higher-priority status fires.
# Only cancel for non-online statuses — "online" is the pulse's own
# target and cancelling it would prevent the pulse from working.
if status != "online":
self.cancel_pending() self.cancel_pending()
with self._coalesce_lock: with self._coalesce_lock:
@@ -497,7 +507,15 @@ class PulseController:
def start(self) -> None: def start(self) -> None:
"""Start the pulse thread.""" """Start the pulse thread."""
logger.info(
"[pulse] start() called, _running=%s, _ddp=%s, _phase=%s, _paused=%s",
self._running.is_set(),
self._ddp is not None,
self._phase,
self._paused.is_set(),
)
if self._running.is_set(): if self._running.is_set():
logger.info("[pulse] start() skip: already running")
return return
# P1-1: Check DDP availability before starting # P1-1: Check DDP availability before starting
@@ -536,12 +554,12 @@ class PulseController:
def pause(self) -> None: def pause(self) -> None:
"""Pause pulsing without stopping the thread (EC-2: reconnect race).""" """Pause pulsing without stopping the thread (EC-2: reconnect race)."""
self._paused.set() self._paused.set()
logger.debug("[pulse] PulseController paused") logger.info("[pulse] PulseController paused")
def resume(self) -> None: def resume(self) -> None:
"""Resume pulsing after pause (EC-2: reconnect race).""" """Resume pulsing after pause (EC-2: reconnect race)."""
self._paused.clear() self._paused.clear()
logger.debug("[pulse] PulseController resumed") logger.info("[pulse] PulseController resumed")
def is_running(self) -> bool: def is_running(self) -> bool:
"""Check if the pulse thread is running.""" """Check if the pulse thread is running."""
@@ -561,12 +579,26 @@ class PulseController:
def _pulse_loop(self) -> None: def _pulse_loop(self) -> None:
"""Background thread: pulse between online and away.""" """Background thread: pulse between online and away."""
logger.info("[pulse] _pulse_loop started, interval=%.1fs", self._pulse_interval)
iteration = 0
while self._running.is_set(): while self._running.is_set():
iteration += 1
# Check if paused # Check if paused
if self._paused.is_set(): if self._paused.is_set():
if iteration == 1 or iteration % 50 == 0:
logger.info(
"[pulse] _pulse_loop iteration=%d: PAUSED (waiting for resume)",
iteration,
)
time.sleep(0.1) time.sleep(0.1)
continue continue
logger.info(
"[pulse] _pulse_loop iteration=%d: calling _pulse_tick, phase=%s, ddp=%s",
iteration,
self._phase,
self._ddp is not None,
)
self._pulse_tick() self._pulse_tick()
# Sleep for pulse interval, checking running flag periodically # Sleep for pulse interval, checking running flag periodically
@@ -579,10 +611,18 @@ class PulseController:
def _pulse_tick(self) -> None: def _pulse_tick(self) -> None:
"""Execute one pulse tick: toggle phase and send status.""" """Execute one pulse tick: toggle phase and send status."""
logger.info(
"[pulse] _pulse_tick fired, phase=%s, ddp=%s, paused=%s, cancel_pending=%s",
self._phase,
self._ddp is not None,
self._paused.is_set(),
self._cancel_pending,
)
# P0-2: Check cancel_pending flag (set by PresenceEngine._set_presence) # P0-2: Check cancel_pending flag (set by PresenceEngine._set_presence)
if self._cancel_pending: if self._cancel_pending:
self._cancel_pending = False self._cancel_pending = False
logger.debug("[pulse] tick skipped (cancel_pending)") logger.info("[pulse] _pulse_tick skip: cancel_pending was set")
return return
# Toggle phase # Toggle phase
@@ -590,9 +630,14 @@ class PulseController:
# Coalesce: skip if same phase as last sent # Coalesce: skip if same phase as last sent
if self._phase == self._last_sent_phase: if self._phase == self._last_sent_phase:
logger.info(
"[pulse] _pulse_tick skip: coalesce (phase=%s == last_sent)",
self._phase,
)
return return
status = "away" if self._phase else "online" status = "away" if self._phase else "online"
logger.info("[pulse] _pulse_tick sending: status=%s", status)
# Try DDP first (K1: DDP set_status has no message variant) # Try DDP first (K1: DDP set_status has no message variant)
if self._ddp is not None and getattr(self._ddp, 'connected', False): if self._ddp is not None and getattr(self._ddp, 'connected', False):
@@ -600,9 +645,10 @@ class PulseController:
self._ddp.set_status(status) self._ddp.set_status(status)
self._last_sent_phase = self._phase self._last_sent_phase = self._phase
self._increment_telemetry() self._increment_telemetry()
logger.info("[pulse] _pulse_tick DDP set_status OK: %s", status)
return return
except Exception as e: except Exception as e:
logger.debug("[pulse] DDP set_status failed: %s", e) logger.info("[pulse] _pulse_tick DDP set_status failed: %s", e)
# REST fallback for message text # REST fallback for message text
if self._rest is not None and getattr(self._rest, 'auth_token', None): if self._rest is not None and getattr(self._rest, 'auth_token', None):
@@ -620,8 +666,15 @@ class PulseController:
) )
self._last_sent_phase = self._phase self._last_sent_phase = self._phase
self._increment_telemetry() self._increment_telemetry()
logger.info("[pulse] _pulse_tick REST setStatus OK: %s", status)
except Exception as e: except Exception as e:
logger.debug("[pulse] REST setStatus failed: %s", e) logger.info("[pulse] _pulse_tick REST setStatus failed: %s", e)
else:
logger.info(
"[pulse] _pulse_tick skip: no DDP and no REST available (ddp=%s, rest=%s)",
self._ddp is not None,
self._rest is not None,
)
def _increment_telemetry(self) -> None: def _increment_telemetry(self) -> None:
"""Increment the heartbeat_pulse telemetry counter. """Increment the heartbeat_pulse telemetry counter.