Fix MetaAgent.remove() leaving stale references on constituent agents#3506
Merged
Fix MetaAgent.remove() leaving stale references on constituent agents#3506
MetaAgent.remove() leaving stale references on constituent agents#3506Conversation
|
Performance benchmarks:
|
|
Performance benchmarks:
|
This was referenced Mar 16, 2026
tpike3
approved these changes
Mar 25, 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.
Pre-PR Checklist
Summary
MetaAgent.remove()leaves stalemeta_agentsandmeta_agentreferences on constituent agents after the meta-agent is removed from the model.Bug / Issue
MetaAgent.__init__setsagent.meta_agents.add(self)andagent.meta_agent = selfon every constituentAgent.remove()only callsmodel.deregister_agent(self)— no cleanup of constituent referencesMetaAgenthad noremove()override, so constituent agents keep dangling pointers to a dead meta-agentImplementation
remove()inMetaAgentto callremove_constituting_agents()on all constituents beforesuper().remove()meta_agentsset removal andmeta_agentpointer updatesget_constituting_agent_instance: was-> set[type], actually returns-> AgentTesting
Added two tests:
test_meta_agent_remove_cleans_up_references— all constituents have clean references after removaltest_meta_agent_remove_with_multiple_memberships— removing one meta-agent preserves the other's references correctlyAll 22 meta-agent tests pass.
Additional Notes
None.
GSoC contributor checklist
Context & motivation
While working on meta-agent bugs from #3184, I traced what happens when a MetaAgent is removed from the model. I realized that
Agent.remove()just deregisters from the model but never cleans up the references thatMetaAgent.__init__wrote onto constituent agents. This means after removal, constituents still think they belong to a dead group. This is a real problem for any model with dynamic group formation and dissolution, which is the core use case for meta-agents.What I learned
The fix turned out to be simple — just override
remove()and call the existingremove_constituting_agents()beforesuper().remove(). The harder part was verifying the edge case where an agent belongs to two meta-agents and one is removed — the surviving references need to stay intact. I also noticed the return type hint onget_constituting_agent_instancewas wrong (set[type]instead ofAgent), so I fixed that too.Learning repo
My learning repo: https://github.com/codebyNJ/GSoC-learning-space
Readiness checks
pytest --cov=mesa tests/)ruff check . --fix)