fix(P1a): Status busy minimum 1s sichtbar — Honeymoon-Timer gegen Coalesce-Überschreibung

This commit is contained in:
Bernd (Hermes)
2026-07-24 09:11:48 +02:00
parent f028a30d6c
commit b40b221896
3 changed files with 327 additions and 11 deletions
+122
View File
@@ -80,8 +80,14 @@ class PresenceEngine:
Thread-safe: uses threading.Lock for all counter operations.
Coalesce-Timer: prevents flickering by delaying status updates by 2s.
Heartbeat: pulses online↔away when background_tasks > 0 and agent is idle.
P1a: Honeymoon-Timer ensures busy status is visible for at least
MIN_BUSY_VISIBLE_S (1.0s) before transitioning to online.
"""
# P1a: Minimum time busy status must be visible (prevents coalesce race)
MIN_BUSY_VISIBLE_S = 1.0
def __init__(
self,
rest=None,
@@ -137,6 +143,12 @@ class PresenceEngine:
self._background_tasks: int = 0
self._background_tasks_lock = threading.Lock()
# P1a: Honeymoon-Timer — busy status minimum 1s sichtbar
self._busy_started_at: float = 0.0 # time.monotonic() when busy started
self._busy_honeymoon_timer: Optional[threading.Timer] = None
self._busy_honeymoon_lock = threading.Lock()
self._busy_counter: int = 0 # multi-turn: count concurrent busy starts
# P0-1: PulseController — DDP-basierte Heartbeat-Pulsierung (1.5s)
self._pulse_controller = PulseController(
pulse_interval=pulse_interval,
@@ -212,6 +224,115 @@ class PresenceEngine:
logger.info("[presence] active_tasks=0 → pulse resumed")
return count
# ── P1a: Honeymoon-Timer — busy minimum 1s sichtbar ──────────────────
def mark_busy_started(self, room_name: str = "") -> None:
"""P1a: Mark the start of a busy period with honeymoon protection.
Sets the room, increments active_tasks, and schedules a busy status.
If a honeymoon timer is already running (multi-turn), only increments
the counter — no duplicate timer.
Args:
room_name: Room name for the busy status message.
"""
self.set_current_room(room_name)
self.increment_active_tasks()
self.update_presence() # sets pending_status="busy"
# P1a: Busy sofort flushen — nicht auf 2s Coalesce warten.
# Der Honeymoon-Timer (1s) würde sonst vor dem Coalesce feuern.
self._flush_presence()
with self._busy_honeymoon_lock:
self._busy_counter += 1
if self._busy_honeymoon_timer is None:
# First busy start — record time and start honeymoon timer
self._busy_started_at = time.monotonic()
self._busy_honeymoon_timer = threading.Timer(
self.MIN_BUSY_VISIBLE_S,
self._honeymoon_expired,
)
self._busy_honeymoon_timer.daemon = True
self._busy_honeymoon_timer.start()
logger.debug(
"[presence] Honeymoon-Timer gestartet (%.1fs), counter=%d",
self.MIN_BUSY_VISIBLE_S, self._busy_counter,
)
else:
logger.debug(
"[presence] Honeymoon-Timer läuft bereits, counter=%d",
self._busy_counter,
)
def mark_busy_done(self) -> None:
"""P1a: Mark the end of a busy period.
Decrements the busy counter. If the counter reaches 0 and the
honeymoon has elapsed, transitions to online immediately.
If the honeymoon hasn't elapsed yet, the timer callback will
handle the transition when it fires.
Must be called in a finally block to ensure counter balance
even on exceptions.
"""
with self._busy_honeymoon_lock:
if self._busy_counter > 0:
self._busy_counter -= 1
counter = self._busy_counter
# Decrement the underlying active_tasks counter
self.decrement_active_tasks()
if counter == 0:
elapsed = time.monotonic() - self._busy_started_at
if elapsed >= self.MIN_BUSY_VISIBLE_S:
# Honeymoon already elapsed — transition to online now
self._cancel_honeymoon_timer()
self.update_presence()
logger.debug(
"[presence] Honeymoon abgelaufen (%.2fs) → online", elapsed,
)
else:
# Honeymoon still running — timer callback will handle it
logger.debug(
"[presence] Honeymoon läuft noch (%.2fs/%.1fs) — Timer übernimmt",
elapsed, self.MIN_BUSY_VISIBLE_S,
)
else:
logger.debug(
"[presence] mark_busy_done: counter=%d (weitere Tasks aktiv)", counter,
)
def _honeymoon_expired(self) -> None:
"""P1a: Honeymoon timer callback — transition to online if idle.
Called by threading.Timer after MIN_BUSY_VISIBLE_S.
If no active tasks remain, sets status to online.
"""
with self._busy_honeymoon_lock:
self._busy_honeymoon_timer = None
with self._active_tasks_lock:
active = self._active_tasks
if active == 0:
logger.info(
"[presence] Honeymoon-Timer abgelaufen, active_tasks=0 → online"
)
self.update_presence()
else:
logger.debug(
"[presence] Honeymoon-Timer abgelaufen, aber active_tasks=%d — kein Wechsel",
active,
)
def _cancel_honeymoon_timer(self) -> None:
"""P1a: Cancel the honeymoon timer if running."""
with self._busy_honeymoon_lock:
if self._busy_honeymoon_timer is not None:
self._busy_honeymoon_timer.cancel()
self._busy_honeymoon_timer = None
def set_onboarding_active(self, active: bool) -> None:
"""Set the onboarding sentinel."""
self._onboarding_active = active
@@ -440,6 +561,7 @@ class PresenceEngine:
"""Clean shutdown: stop PulseController, DetectionTimer, cancel coalesce timer."""
self._pulse_controller.stop()
self._detection_timer.stop()
self._cancel_honeymoon_timer()
with self._coalesce_lock:
if self._coalesce_timer is not None:
self._coalesce_timer.cancel()