from __future__ import annotations import time def now_ms() -> int: return int(time.time() * 1000) def fade_confidence(elapsed_ms: int, fade_threshold_seconds: int) -> float: """ Convert elapsed inactivity to a confidence score for fading state. Confidence starts at 0.7 when fading begins and decays toward 0.2 over 4x fade threshold windows. """ threshold_ms = max(1, int(fade_threshold_seconds or 900) * 1000) if elapsed_ms <= threshold_ms: return 0.7 over = min(4.0, float(elapsed_ms - threshold_ms) / float(threshold_ms)) return max(0.2, 0.7 - (over * 0.125)) def should_fade(last_event_ts: int, now_ts: int, fade_threshold_seconds: int) -> bool: if last_event_ts <= 0: return False threshold_ms = max(1, int(fade_threshold_seconds or 900) * 1000) return int(now_ts) - int(last_event_ts) >= threshold_ms