Skip to content

meta-agents refactor from id to agent object - #3875

Merged
tpike3 merged 21 commits into
mesa:mainfrom
tpike3:meta-agent-by-object
Oct 18, 2026
Merged

tpike3 merged 21 commits into
mesa:mainfrom
tpike3:meta-agent-by-object

Conversation

@tpike3

@tpike3 tpike3 commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Migrate the meta-agent MembershipBackend from storing unique_id integers to storing agent objects directly, aligning it with how AgentSet, Agent, and Model handle agents throughout the rest of Mesa.

Motive

The backend was introduced using unique_id integers as the canonical identity for membership edges. This was inconsistent with the rest of Mesa's core, where agent objects are passed and stored directly. The mismatch forced the API layer (MetaAgents) to maintain an expensive O(N) reverse-lookup (_live_entity_lookup) on every call to resolve IDs back to live agent objects. Switching to objects removes this indirection, simplifies the code, and brings meta-agents in line with the established patterns.

Implementation

Backend (backend.py):

  • Removed _to_id() which extracted unique_id from agent objects. The backend now stores whatever hashable entity is passed in — agent objects for Mesa agents, plain strings/ints for standalone usage.

API layer (meta_agents_api.py):

  • Removed _entity_id(), _live_entity_lookup(), and _resolve_entity() — the entire ID-to-object resolution layer.
  • Simplified _resolve_group(), _resolve_view(), members_of(), groups_of(), query_memberships(), at_level(), and dissolve() to work with agent objects directly from the backend.

Tests:

  • Updated backend assertions from unique_id-based to object-based across test_meta_agents_backend.py, test_meta_agents_api.py, and test_alliance_formation_model.py. Reduce number of test to focus only Public API

Usage Examples

N/A — The public API (create, add_member, remove_member, members_of, groups_of, etc.) is unchanged. This is a backend storage change. The only difference is that backend.as_triplets(), backend.groups_of(), and backend.agents_of() now return agent objects instead of unique_id integers.

@github-actions github-actions Bot added the testing Release notes label label Sep 19, 2026
@tpike3 tpike3 mentioned this pull request Sep 19, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.6% [+0.0%, +1.2%] 🔵 +0.5% [+0.3%, +0.7%]
BoltzmannWealth large 🔵 +2.0% [+1.3%, +2.5%] 🔴 +12.1% [+9.3%, +14.8%]
Schelling small 🔵 +2.9% [+2.5%, +3.4%] 🔵 +1.3% [+0.5%, +2.2%]
Schelling large 🔵 +2.3% [+1.6%, +3.0%] 🔴 +12.6% [+10.4%, +15.0%]
WolfSheep small 🔵 +0.6% [+0.1%, +1.0%] 🔵 +0.2% [-3.7%, +4.4%]
WolfSheep large 🔵 +2.0% [+1.2%, +2.7%] 🔴 +7.2% [+5.8%, +8.5%]
SugarscapeG1mt small 🔵 +0.2% [-0.3%, +0.7%] 🔵 -2.1% [-3.6%, -0.7%]
SugarscapeG1mt large 🔵 -0.7% [-1.6%, +0.4%] 🔵 -1.8% [-3.4%, -0.4%]
BoidFlockers small 🔵 -2.6% [-3.2%, -1.9%] 🔵 -0.8% [-1.3%, -0.2%]
BoidFlockers large 🔵 -2.4% [-3.0%, -2.0%] 🔵 -1.4% [-1.8%, -1.1%]
MultiLevelAllianceModel small 🔵 -2.5% [-3.8%, -1.3%] 🟢 -10.0% [-10.4%, -9.6%]
MultiLevelAllianceModel large 🔵 -3.6% [-4.5%, -2.7%] 🟢 -7.4% [-9.1%, -5.5%]

@jackiekazil jackiekazil self-assigned this Sep 25, 2026

@jackiekazil jackiekazil left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This all looks good. Great speed up.

I have two questions --

  1. How much to support backwards compatibility? I am under the impression that with 4.0 causing breaking changes at this point we shouldn't. But I could be talked back.

  2. (Separate, but related to this PR) Should meta-agents live where it is living or like the behavioral framework - should we create a mix-in for this and then create a general mix-ins folder that holds all these mix-ins? (In the future when we have 100 - maybe these are broken off into their own libraries for whatever work one is doing.)

Comment thread mesa/meta_agents/meta_agents_api.py Resolved
Comment thread mesa/meta_agents/meta_agents_api.py Resolved
@tpike3

tpike3 commented Sep 26, 2026

Copy link
Copy Markdown
Member Author

This all looks good. Great speed up.

I have two questions --

  1. How much to support backwards compatibility? I am under the impression that with 4.0 causing breaking changes at this point we shouldn't. But I could be talked back.
  2. (Separate, but related to this PR) Should meta-agents live where it is living or like the behavioral framework - should we create a mix-in for this and then create a general mix-ins folder that holds all these mix-ins? (In the future when we have 100 - maybe these are broken off into their own libraries for whatever work one is doing.)

Regarding 1 - Yes the backwards capability was overkill on my part, particularly because it hasn't been part of a release; removed

Regarding 2 - Good point, and this is a big decision, maybe I was avoiding to meet the GSOC deadline and went overly aggressive moving it out. However, do we want to go to the monorepo like polars; huggingface or put them in a directory like .ext (sqlalchemy) or .contrib (django). @EwoutH. @wang-boyu and @quaquel -- What are your thoughts, do you already have a plan?

@github-actions github-actions Bot added example Changes the examples or adds to them. experimental Release notes label labels Sep 30, 2026
@github-actions github-actions Bot added the docs Release notes label label Sep 30, 2026
@EwoutH

EwoutH commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

Sorry for the late reply. Thanks for working on this.

In time, meta-agents can be a core Mesa feature, if it's up to me.

Otherwise I'm not against any of the other ideas, they all make sense in some way. We could do some experimentation with contrib (not sure we're big enough for that yet) and for really large things I can also see value in mono-repo, multiple packages. Or even in multi-repo in some cases. Sub-namespace (contrib/ext) sounds like something to explore though, sounds like very low overhead!

But for meta-agents, I think it's cool, useful to a large enough audience, and sets mesa apart. My 2 cents: let's aim to make it core feature.

@EwoutH EwoutH left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few higher-level questions, mostly on design and API rather than implementation:

1. What can be stored?
The backend accepts anything hashable (strings, ints, objects). The API is meant for agents. Can we make the API accept only agents in the model, and raise an error otherwise? Right now add_member(group, agent.unique_id) silently stores an int instead of failing.

2. Can we use the public signal?
Meta-agents uses a private hook (_agent_removed_hooks) to learn when an agent is removed. The model already emits ModelSignals.AGENT_REMOVED for this. Can meta-agents use that instead? If not, the signal system is missing something, and we should fix that.

CC @quaquel, is signals ready enough?

3. Are removed agents fully cleaned up?
The backend now holds direct references to agents, and queries no longer filter out removed ones. Could you add a test that a removed agent (and a removed group) is garbage-collected and no longer appears in members_of / groups_of?

4. Is the order reproducible?
Results are sorted by str(agent). If users override __str__, ties can occur, and the order then depends on memory addresses. That breaks runs with a fixed seed. Can we sort by unique_id instead?

5. What does each removal method do?
There's remove_member, deactivate, dissolve, and agent.remove(). What exactly happens after each one? If that's hard to explain in a few lines, we may have one too many.

6. Can the API be smaller?

  • Do we need lookup by group name? Nothing else in Mesa does that.
  • Why do add/remove return a MembershipView, while queries return an AgentSet?
  • find_combinations and evaluate_combination are about finding coalitions, not managing memberships. Could they move to the example?
  • relation can be any value ("member", "leader", ...). Is that intended? Or do we want a set of special types with certain, defined meanings? Worth discussing a bit I think.

7. PR housekeeping

  • The description still mentions _resolve_to_agent(), which was removed.
  • Could you keep only tests of the public API? Tests of private helpers or specific line numbers break easily.
  • Some benchmarks show 7–12% slowdowns on models that don't use meta-agents. Probably noise, but could you rerun them?

@tpike3 tpike3 closed this Oct 6, 2026
@tpike3

tpike3 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member Author

Thanks @EwoutH! - Responses below ---

1. What can be stored? -- Good catch I missed then when I did the transition, I will fix it

2. Can we use the public signal? -- This is an artifact of moving it in and out of experimental and I forgot to make this change. I will switch it, but then meta-agents should stay in experimental until signals comes out, which was the original reason not to use it. Also speaks to 6.

3. Are removed agents fully cleaned up? - Yes, and I will add the test

4. Is the order reproducible? - Makes sense, I can do that

5. What does each removal method do? (hmmm I will relook the doc string to put them in context of meta-agents)
--remove: removes link between member agent and meta-agent
--dissolve: removes all members form a meta-agent and removes the meta-agent form the model
--deactivate: removes all members but keeps the meta-agent
--agent.remove: atomic agent removed form model, hook triggers removal of memberships from meta-agent
This also speaks to 6.

6. Can the API be smaller? This gets back to Mesa organization. The idea/plan was make an extensible API so as meta-agents is employed new and more complex features can be added with the core abstraction of meta-agents as adaptive networks being the same. For me its critical to store these ideas from open source and an extensible API becomes the way to do that. To your point, if I am understanding correctly, what is the organization and how do you keep it clean? I am open to ideas as this is directly related to the larger issue of Mesa's organization.

7. PR housekeeping Good catch on resolve_to_agent I will remove, thanks!; 100% on the tests! Just know my codecov will fail. I have run it a couple times, and looked into, from my look it was 12% on microseconds so noise.

@tpike3 tpike3 reopened this Oct 6, 2026
@tpike3

tpike3 commented Oct 11, 2026 •

Copy link
Copy Markdown
Member Author

I addressed many of @EwoutH points, some of the like size of API etc I did not address pending further discussion of Mesa organization.

Can I please get an approval to merge

@jackiekazil jackiekazil left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. If there are additional changes, let's open up a new PR.

@tpike3
tpike3 merged commit 2b2c925 into mesa:main Oct 18, 2026
15 of 16 checks passed
@tpike3
tpike3 deleted the meta-agent-by-object branch October 18, 2026 12:54
tpike3 added a commit that referenced this pull request Oct 20, 2026
#3875 moved meta-agents back to mesa.experimental.meta_agents, but the
migration guide and the API page still import from mesa.meta_agents,
which no longer exists. Copying the examples fails with
ModuleNotFoundError, and the API page's automodule can't import the
module, so the reference section is empty on the docs site.

Update the imports and the automodule target, and fix the migration
guide heading, which said meta-agents moved out of experimental.

Co-authored-by: Tom Pike <[email protected]>
quaquel pushed a commit to mesa/mesa-examples that referenced this pull request Oct 28, 2026
The Meta-Agents Rewrite (mesa/mesa#3811, mesa/mesa#3875) moved the package
from mesa.meta_agents to mesa.experimental.meta_agents, and mesa no longer
exports meta_agents at the top level. The warehouse example still imported
the old path:

    from mesa.meta_agents import MetaAgents

Since test_examples.py imports every model module, that ModuleNotFoundError
aborts collection, so the whole examples suite fails before a single test
runs. That is the examples job currently red on mesa main, and therefore on
every open mesa PR.

mesa's own migration guide gives the new path: "Meta-agents still live in
mesa.experimental.meta_agents". Nothing else in the example needs changing,
since the rest already uses the rewritten API through model.meta_agents.

Against mesa main, test_examples.py goes from 1 collection error, 0 tests run
to 24 passed / 1 failed. The remaining failure is VirusAntibodyModel, a
separate pre-existing bug fixed by #496.
@EwoutH EwoutH mentioned this pull request Nov 15, 2026
53 of 65 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Release notes label example Changes the examples or adds to them. experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants