Bug Description
MovingAverage.reset() in livekit-agents/livekit/agents/utils/moving_average.py resets _count and _sum to zero but does not clear the _hist ring-buffer. This causes stale values from before the reset to corrupt the moving average once the window fills up again.
Root Cause
def reset(self) -> None:
self._count = 0
self._sum = 0
# BUG: self._hist is not cleared!
After reset(), as add_sample() is called:
_count increments from 0, _sum is rebuilt correctly until the window is full.
- Once
_count > len(self._hist), the code subtracts self._hist[index] from _sum to evict the oldest entry. But _hist[index] still contains the pre-reset value, so the sum is incorrectly reduced by that stale value.
Fix
Clear _hist in reset():
def reset(self) -> None:
self._hist = [0.0] * len(self._hist)
self._count = 0
self._sum = 0.0
Impact
MovingAverage is used in metrics tracking across the agents framework. Incorrect averages after a reset can lead to skewed performance metrics and incorrect adaptive behaviour.
Bug Description
MovingAverage.reset()inlivekit-agents/livekit/agents/utils/moving_average.pyresets_countand_sumto zero but does not clear the_histring-buffer. This causes stale values from before the reset to corrupt the moving average once the window fills up again.Root Cause
After
reset(), asadd_sample()is called:_countincrements from 0,_sumis rebuilt correctly until the window is full._count > len(self._hist), the code subtractsself._hist[index]from_sumto evict the oldest entry. But_hist[index]still contains the pre-reset value, so the sum is incorrectly reduced by that stale value.Fix
Clear
_histinreset():Impact
MovingAverageis used in metrics tracking across the agents framework. Incorrect averages after a reset can lead to skewed performance metrics and incorrect adaptive behaviour.