Repository navigation
feat(agentset): warn when one mutable object is shared across agents - #3920
Merged
quaquel merged 2 commits intoOct 28, 2026
Merged
Conversation
|
Performance benchmarks:
|
Member
|
can you take a look at the merge conflicts? |
Contributor
Author
|
sure give me a few |
Signed-off-by: Rudra Dudhat <[email protected]>
Signed-off-by: Rudra Dudhat <[email protected]>
quaquel
reviewed
Oct 28, 2026
| "it on one agent will change it for every agent that shares it; " | ||
| "pass a separate copy per agent if that is not what you want.", | ||
| UserWarning, | ||
| stacklevel=4, |
Contributor
Author
There was a problem hiding this comment.
its the amount of hops between the users code and warnings.warn(), the flow is like this
- warnings.warn()line itself line 131.
- _resolve_per_agent_values, line 105.
- set() line 468.
- user's code that called set()
RudraDudhat2509
force-pushed
the
warn-shared-per-agent-objects
branch
from
October 28, 2026 15:41
f2775aa to
673fc65
Compare
Contributor
Author
|
merge conflict should be fixed on the latest push, thanks |
quaquel
approved these changes
Oct 28, 2026
Contributor
Author
|
thx for the review ! |
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
Approval Link
#3863 (quaquel: "document the behavior in the docstring and, perhaps, issue a single warning if the object is identical")
Summary
When one builtin mutable container (a list, dict, set, or bytearray) is passed as a per-agent sequence to
create_agentsorAgentSet.set, every agent that receives it shares the same object. This PR adds a singleUserWarningfor that case, and documents that per-agent values are assigned by reference.No copying is done by default.
Motive
[dna] * 20looks like a fine way to give 20 agents a starting value. It isn't, when the value is mutable and an agent changes it in place. This is the bug behind mesa-examples#496, where the virus model needed a manualcopy.deepcopyper agent to stop all viruses sharing one DNA list.Implementation
_resolve_per_agent_valuescalls a new helper,_warn_if_object_shared_between_agents, when a sequence matches the agent count.list,dict,set,bytearray). It warns on the first one it sees twice. One warning per call.WolfSheepexample does exactly this.stacklevel=4, so the warning points at the caller's line, not at mesa internals._resolve_per_agent_valuesandAgentSet.setnow say values are assigned by reference.Usage Examples
Additional Notes
spacereference is normal.tests/test_agentset.py::test_set_warns_when_one_mutable_object_is_shared_by_agents. Red before the change, green after.tests/test_agentset.pyandtests/test_agent.pypass (59 tests).ruff checkandruff format --checkare clean.