Repository navigation
Meta-Agents Rewrite - #3811
Meta-Agents Rewrite#3811
Conversation
d0ffd95 to
58e2dda
Compare
|
Performance benchmarks:
|
010b22d to
b1c69df
Compare
…backend-authoritative architecture
b1c69df to
59a1a12
Compare
|
Performance benchmarks:
|
tpike3
left a comment
There was a problem hiding this comment.
@falloficaruss This is really some great work! The membership backend of storing the triplets, ensuring there can only be one, and the assert invariance makes meta-agents much better.
I think the biggest challenge is the lookup bottleneck in the meta_agents_api. So take a look at that, but unless I am missing something I don't think it is leveraging membership backend like it could be.
Overall though great work in moving meta-agents forward.
…MetaAgents with a clean break
…ld option Note that the live entity lookup should be cached and only rebuilt on add or remove calls, with bulk adds and removes as a future optimization, plus a cache_build staticmethod as a future iteration. Co-Authored-By: Claude Opus 5 <[email protected]>
I have an initial idea of how we could handle this but would need some more thought. We should do this after this PR. |
Defaults to Agent when omitted. Docstring, tests, and docs updated to demonstrate the omitted-argument form.
Document future work for statically typing dynamically generated meta-agents and reducing Any usage at the creation boundary. Co-Authored-By: Claude Opus 5 <[email protected]>
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.
Note
This PR is a part of GSoC 2026 project "Hypergraph-Based Meta-Agents for Mesa"
Related: #3016, #3403 mesa/mesa-examples#483
A detailed write-up is available here
Summary
A complete rewrite of Mesa's
meta-agentcapability. Meta-agents are agents composed of other agents (alliances, households, teams, org units) with overlapping, typed, and nested memberships. This PR replaces the old experimental implementation where each group object held its own member set and membership state was duplicated on member agents with a backend-authoritative architecture: all memberships live as(member, group, relation)triplets in a single store on the model, and every read/write goes through oneMetaAgentsmanager. The module is moved out frommesa.experimental.meta_agentsto a top-levelmesa.meta_agentspackage (the old API is removed outright, consistent with the Mesa 4.0 breaking-change window).Motive
The old implementation scattered membership state across live objects (
meta_agent.constituting_agents,agent.meta_agent,agent.meta_agents), which made overlapping and nested hierarchies a bolt-on rather than a structural property, gave no single source of truth for invariants, and offered no indexed queries. The rewrite follows @tpike3's direction: a hypergraph-style conceptual basis, a canonical incidence backend, and a facade-style public API making overlap, typed relations, and multi-level containment first-class while keeping groups as ordinary Mesa agents that participate in scheduling, datacollection, and visualization.Implementation
Three layers under
mesa/meta_agents/:MembershipBackend(backend.py) :(member_id, group_id, relation)triplets with bidirectional_by_agent/_by_groupindexesunique_id, so the store never holds live-object references (no dangling pointers when agents die)replace_relation, bulk add, cascaderemove_agent/remove_group, andassert_invariantsfor testsMetaAgentsmanager (meta_agents_api.py) : the public API, installed once per model (model.meta_agents = MetaAgents(model)) and the intended only write path:create(...)builds a live group agent (dynamic subclass of user-supplied or existing model classes, withmeta_attributes/meta_methods) and records edges in bulkadd_member/remove_memberaccept live objects,unique_ids, or group namesmembers_of/groups_ofreturn liveAgentSets, optionally filtered by relationquery_membershipsreturns an immutableMembershipViewof resolvedMembershipEdge(s)deactivate(detach all edges, keep the object) vsdissolve(detach and remove from the model)at_level(n, root=...)- BFS containment depth with shallowest-path semantics; cycle-safeModelSignals.AGENT_REMOVEDso memberships are deactivated automatically when an agent leaves the modelInternal
MetaAgent+ discovery helpers (meta_agent.py) a thinAgentsubclass bound to the manager (itsremove()routes throughdissolve);find_combinations/evaluate_combinationare kept as pure, non-mutating discovery helpers.Key design decisions:
"member") allow several roles between the same agent–group pairUsage Examples
The in-tree alliance formation model (moved experimental -> advanced) exercises the full surface: agents form alliances via a bilateral Shapley evaluation, alliances become meta-agents that form higher-level alliances, with overlapping memberships tracked by the manager and rendered as a multi-level network.
Additional Notes
mesa-examples: Update warehouse example to the new meta-agents API mesa-examples#483Future Scope & Limitations
createalways create a new group with an explicit join path (today it is join-or-create when a same-class group exists; the alliance example works around this with unique class names)model.agentsper call)