Enforce monotonic time invariant in event scheduling#3343
Merged
Conversation
quaquel
reviewed
Feb 18, 2026
| raise ValueError( | ||
| f"Cannot schedule event at time {time}, current time is {self.time}" | ||
| ) | ||
| event = Event(time, function, priority=priority) |
Member
There was a problem hiding this comment.
Suggested change
| event = Event(time, function, priority=priority) | |
| raise ValueError( | |
| f"Cannot schedule event in the past. Scheduled time is {time}, but current time is {self.time}" | |
| ) |
quaquel
reviewed
Feb 18, 2026
Comment on lines
+428
to
+431
| raise ValueError( | ||
| f"Cannot start recurring schedule at time {schedule.start}, " | ||
| f"current time is {self.time}" | ||
| ) |
Member
There was a problem hiding this comment.
Suggested change
| raise ValueError( | |
| f"Cannot start recurring schedule at time {schedule.start}, " | |
| f"current time is {self.time}" | |
| ) | |
| raise ValueError( | |
| f"Cannot start recurring schedule in the past. Start time is {schedule.start}, " | |
| f"current time is {self.time}" | |
| ) |
quaquel
requested changes
Feb 18, 2026
Member
quaquel
left a comment
There was a problem hiding this comment.
Looks good to me. Please look at the suggested change to the message and add raises to the docstring.
|
Performance benchmarks:
|
0f89f9a to
235f4f6
Compare
Contributor
Author
|
@quaquel i made the changes you recommended, thanks for the review. |
quaquel
approved these changes
Feb 18, 2026
Krishsharma179
pushed a commit
to Krishsharma179/mesa
that referenced
this pull request
Feb 21, 2026
42 tasks
EwoutH
pushed a commit
that referenced
this pull request
Mar 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR enforces a monotonic time invariant in the event scheduling system. It prevents one-off and recurring events from being scheduled at times earlier than the model’s current simulation time. This ensures the model clock cannot move backwards during execution.
Fixes #3342
Bug / Issue
It was previously possible to run the model forward (e.g., to time 10), schedule a new event at an earlier time (e.g., time 3) and then execute that event, causing the model clock to move backward. There is no rollback mechanism in the execution model, so this behavior violates expected time semantics.
Implementation
The fix enforces the invariant at the scheduling boundary:
Model.schedule_event, aValueErroris raised if time <self.timeModel.schedule_recurring, aValueErroris raised ifschedule.startis not None andschedule.start<self.timeThis ensures both one-off and recurring schedules cannot be initialized in the past. This is enforced at the API boundary rather than inside
_advance_time, preventing invalid events from entering the event list in the first place.Testing
Added tests in
tests/test_model.pyfor verification. Normal forward scheduling behavior remains unchanged All time-related tests pass.Additional Notes
This change formalizes time monotonicity as an invariant of the scheduling system. It does not alter normal forward execution behavior. This preserves consistency with the discrete event execution model, which does not support rollback or reversible time progression.