Skip to content

EventList.peak_ahead() returns events in wrong order #3009

@Nithin9585

Description

@Nithin9585

Describe the bug

The peak_ahead(n) method in EventList iterates directly over the internal heap list (self._events), which is not sorted. A binary heap only guarantees the minimum element at index 0; the rest is in heap-structure order, not chronological order.

Location: mesa/experimental/devs/eventlist.py, method peak_ahead()


Expected behavior

peak_ahead(n) should return the next n events in chronological order (sorted by time, priority, then unique_id).

Example: Events at times [5, 8, 10, 15, 20] - peak_ahead(5) should return them in that order.

Actual behavior: Returns [5, 15, 8, 25, 20] (heap storage order, not sorted).


Proposed Fix

Use heapq.nsmallest() instead of iterating the heap list directly.


Additional context

  • Impact: Debugging tools, UI dashboards, and AI look-ahead get wrong event order.
  • Note: pop_event() works correctly bug only affects the inspection API.
  • Typo: Method should be peek_ahead (peek = look), not peak_ahead .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions