Skip to content

Add missing mesa-internal attributes to mesa_primitives blocklist in create_meta_agent#3538

Open
codebyNJ wants to merge 8 commits intomesa:mainfrom
codebyNJ:fix/meta-agent-mesa-primitives-blocklist
Open

Add missing mesa-internal attributes to mesa_primitives blocklist in create_meta_agent#3538
codebyNJ wants to merge 8 commits intomesa:mainfrom
codebyNJ:fix/meta-agent-mesa-primitives-blocklist

Conversation

@codebyNJ
Copy link
Copy Markdown
Contributor

@codebyNJ codebyNJ commented Mar 14, 2026

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Fixes add_attributes in create_meta_agent so existing meta-agent attributes are not overwritten by values inferred from constituting agents. Uses hasattr() checks to preserve existing attributes—both internal Mesa attributes and explicitly-provided ones.

Bug / Issue

When assume_constituting_agent_attributes=True, the previous implementation unconditionally copied all public non-callable attributes from constituent agents onto the meta-agent, silently overwriting:

  1. Mesa internal attributes (cell, current_action, meta_agent, meta_agents)
  2. Explicitly-passed meta_attributes (as reported in preserve explicit meta attributes/methods over inferred members #3472)

Expected behavior: don't overwrite attributes that already exist on the meta-agent.
Actual behavior: any existing attribute could be silently overwritten without warning.

Related context: issues #3433, #3506, #3472 all identified meta-agent attribute problems.

Implementation

Updated add_attributes helper inside create_meta_agent in mesa/experimental/meta_agents/meta_agent.py:

  • When building meta_attributes from agents: Check not hasattr(meta_agent_instance, name) before copying
  • When applying meta_attributes to meta-agent: Check if not hasattr(meta_agent_instance, key) before calling setattr()

Why this is better than a blocklist:

Testing

Added test_assume_attributes_does_not_overwrite_existing_attributes:

  • Verifies explicit meta_attributes are preserved when adding agents with assume_constituting_agent_attributes=True
  • Verifies missing attributes are still copied from constituents

Additional Notes

This change keeps behavior predictable: existing attributes are preserved, inferred values only fill missing keys.

No API changes are required for current users.

This complements determinism-related fixes (#3433, #3506) and does not replace them.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.0% [-1.4%, -0.7%] 🔵 -0.1% [-0.3%, +0.0%]
BoltzmannWealth large 🔵 -0.3% [-0.8%, +0.0%] 🔵 -0.9% [-1.7%, -0.2%]
Schelling small 🔵 +0.3% [-0.2%, +0.8%] 🔵 +0.3% [-0.4%, +1.0%]
Schelling large 🔵 -1.4% [-2.0%, -0.8%] 🔵 -2.2% [-3.2%, -1.1%]
WolfSheep small 🔵 +0.1% [-0.5%, +0.7%] 🔵 +2.3% [-1.5%, +6.1%]
WolfSheep large 🔵 +0.1% [-0.8%, +1.2%] 🔵 +1.8% [+0.7%, +2.9%]
SugarscapeG1mt small 🔵 +0.1% [-0.3%, +0.5%] 🔵 +2.7% [+1.1%, +4.4%]
SugarscapeG1mt large 🔵 +0.9% [-0.5%, +2.4%] 🔵 +1.3% [-0.9%, +3.4%]
BoidFlockers small 🔵 +1.3% [+0.6%, +2.1%] 🔵 +2.0% [+1.6%, +2.5%]
BoidFlockers large 🔵 +2.7% [+1.8%, +3.9%] 🔵 +2.0% [+1.2%, +3.1%]

@tpike3
Copy link
Copy Markdown
Member

tpike3 commented Mar 18, 2026

As this is clearly a bug and there are some related issues as identified in #3472, instead of both #3538 and #3472 why dont we just ensure the meta-agent doesn't overwrite attributes it already has.

This approach

 mesa_primitives = [
            "unique_id",
            "model",
            "pos",
            "name",
            "random",
            "rng",
            "cell",
            "current_action",
            "meta_agent",
            "meta_agents",
        ]

is very brittle, and so issues identified in #3472 and #3538, plus the current brittleness can all be solved with not overwriting existing meta-agent attributes.

@falloficarus22
Copy link
Copy Markdown
Contributor

falloficarus22 commented Mar 18, 2026

As this is clearly a bug and there are some related issues as identified in #3472, instead of both #3538 and #3472 why dont we just ensure the meta-agent doesn't overwrite attributes it already has.

This approach

 mesa_primitives = [
            "unique_id",
            "model",
            "pos",
            "name",
            "random",
            "rng",
            "cell",
            "current_action",
            "meta_agent",
            "meta_agents",
        ]

is very brittle, and so issues identified in #3472 and #3538, plus the current brittleness can all be solved with not overwriting existing meta-agent attributes.

This is a better direction for the attribute side. It seems more robust than extending the mesa_primitives blocklist, and it also looks like it would cover both the internal-attribute corruption in this PR and the explicit-attribute overwrite case in #3472. I do think methods are still a separate question, since inferred method-name collisions are not addressed by the same rule, but for attributes this feels like the cleaner approach.

@codebyNJ
Copy link
Copy Markdown
Contributor Author

@tpike3 @falloficarus22 I have updated the PR.

Copy link
Copy Markdown
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @codebyNJ , this looks good to me, can you fix the test conflicts and then I will merge.

@codebyNJ
Copy link
Copy Markdown
Contributor Author

Thanks @codebyNJ , this looks good to me, can you fix the test conflicts and then I will merge.

Welcome @tpike3. I have fixed the merge conflict issues in the test files.

@tpike3
Copy link
Copy Markdown
Member

tpike3 commented Mar 29, 2026

@codebyNJ Could you look at the Warehouse Model failure. The warehouse model is the other meta-agent example so we need to understand why this code broke it. https://github.com/mesa/mesa-examples/tree/main/examples/warehouse

@codebyNJ
Copy link
Copy Markdown
Contributor Author

sure @tpike3 i will look into warehouse model and find out the reason why this code broke.

@codebyNJ
Copy link
Copy Markdown
Contributor Author

@tpike3 Fixed the warehouse failure. The issue: hasattr() check prevented explicit meta_attributes (like cell) from overriding class defaults.

Solution: Always apply explicit meta_attributes (user intent takes precedence).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants