Repository navigation
Skip the scheduled events of removed agents - #4119
Merged
Merged
Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
A removed agent's events, recurring events and Threshold events only stopped once Python freed the agent, which happens late or never: an agent that was still referenced kept acting, and one in a reference cycle acted until the cyclic garbage collector ran, so results for a given seed depended on garbage collection. Agent.remove() now marks the agent as removed. An event on a removed agent's method is skipped when it comes up, a generator stops, and a Threshold doesn't fire. Fixes mesa#4117 Assisted-by: Claude Code Assisted-by: GPT-6
EwoutH
force-pushed
the
skip-removed-agent-events
branch
from
November 16, 2026 21:56
7c1f830 to
f402539
Compare
EwoutH
added a commit
that referenced
this pull request
Nov 22, 2026
Adapted for 3.5.x: the Agent.remove() and Event/EventGenerator parts only. 3.5 has no experimental states (Threshold) or locations, so those hunks and their tests are dropped. (cherry picked from commit eac7447) Assisted-by: Claude Opus 5.5
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.
Pre-PR Checklist
Summary
A removed agent no longer acts through the scheduler: its pending events are skipped, its recurring events stop and its
Thresholddoesn't fire. Results no longer depend on garbage collection. This is option 2 from #4117, which was preferred over the bookkeeping approach in #4118.It also makes the documentation true: the DES tutorial and the
Eventdocstring already say a removed agent's events do nothing, but that only held once Python had freed the agent.Bug / Issue
Fixes #4117. Scheduled events only stopped once Python freed the removed agent: never if it was still referenced, and at a GC-dependent moment if it was in a reference cycle.
Implementation
Agentgets a class attribute_removed = False.remove()sets it toTrue.Event.executeskips the callback when it's a method of an object whose_removedis true.EventGeneratorstops in that case.Threshold.executereturns early for a removed agent, because its callback is a closure, not a method.Testing
test_schedule_run.py(one-off, recurring and post-removal events of a still-referenced agent don't run) and one intest_states.py(a removed wolf doesn't starve). Both fail on main.schedule_recurringThresholdagents dying and respawningAdditional Notes
Compared to #4118:
mesa/, no per-agent bookkeeping.Thresholdthat re-arms because a removed agent's state changed. Cancel a removed agent's scheduled events #4118 misses both.CANCELED.Like #4118, this changes behaviour: any event on a removed agent's method is skipped, whoever scheduled it.
functools.partialcallbacks aren't covered.AI-assisted: implemented and benchmarked with Claude Code.