Repository navigation
Conversation
Agent.remove() didn't cancel the events, recurring events or Threshold events scheduled on the agent's methods. They only stopped once Python freed the agent, which happens late or never: a removed agent that is 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. The model now records events and generators scheduled on an agent's method on that agent, and remove() cancels them. Entries are dropped when an event runs or is canceled, or a generator stops. Threshold schedules a closure, so it records its event explicitly. Fixes mesa#4117 Assisted-by: Claude Code
1 task done
|
Performance benchmarks:
|
Member
Author
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
Agent.remove()now cancels the events, recurring events andThresholdevents scheduled on the agent, so a removed agent stops acting, and results no longer depend on garbage collection. This is option 1 from #4117. Draft until we agree on the direction there.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
Model.schedule_eventandschedule_recurringrecord the event or generator on the agent when the callback is one of its methods (function.__self__is anAgent), in a newAgent._scheduleddict.Agent.remove()cancels the events and stops the generators in that dict.Thresholdschedules a closure, not a method, so it records its event explicitly.Weak references stay as they are, against memory leaks.
Testing
test_schedule_run.py(events and generators of a still-referenced agent are canceled, finished events are released) and one intest_states.py(a removed wolf doesn't starve). All three fail on main.schedule_recurringThresholdagents dying and respawningAdditional Notes
Thresholdthat re-arms because a removed agent's state changed. Callbacks that aren't bound methods (functools.partial, plain functions) aren't tracked either.AI-assisted: implemented and benchmarked with Claude Code.