Skip to content

Fix StopIteration when reusing a meta-agent group name after dissolve - #3850

Closed
shipitdev wants to merge 1 commit into
mesa:mainfrom
shipitdev:fix/extract-class-empty-bucket
Closed

shipitdev wants to merge 1 commit into
mesa:mainfrom
shipitdev:fix/extract-class-empty-bucket

Conversation

@shipitdev

Copy link
Copy Markdown
Contributor

Ref #3849.

Problem

Create a meta-agent group, dissolve it, create another with the same name → StopIteration.

extract_class looks the class up by name, then reads the type back off a live instance. Dissolving the last group of a name empties that agents_by_type bucket but keeps the key, so the name lookup still succeeds, the None early-return doesn't fire, and next(iter(...)) runs on an empty set.

Change

     agent_type_names = {
         agent_type.__name__: agent_type for agent_type in agents_by_type
     }
-    agent_type = agent_type_names.get(new_agent_class)
-    if agent_type is None:
-        return None
-    return type(next(iter(agents_by_type[agent_type])))
+    return agent_type_names.get(new_agent_class)

Model.register_agent keys the dict by exact type, so the existing name lookup already yields the class and the instance round-trip was redundant. Returning it directly gives the same object for every populated bucket — including the duplicate-name case, where the comprehension's last-wins behaviour is unchanged and no longer needs a live instance to exist.

Alternatives

Happy to go another way

Opening as a draft since #3849 is still open for discussion glad to reshape this whichever way you prefer.

Tests

Three added to tests/test_meta_agents_backend.py: reuse after dissolve doesn't raise; the original class is reused rather than re-created; and extract_class resolves a known name and returns None for an unknown one. The first two fail with StopIteration on main; the third passes either way, to show the happy path is unchanged.

Full suite: 791 passed. Four don't pass in my environment (missing pyarrow and some optional viz deps) — identical results with and without this change.

Creating a group, dissolving it, then creating another group with the same
name raised StopIteration.

extract_class looked up the class by name and then read the type off a live
instance. Dissolving the last group of a name empties its agents_by_type
bucket but keeps the key, so the name lookup still succeeded and
next(iter(...)) then ran on an empty set.

agents_by_type is keyed by exact type in Model.register_agent, so the name
lookup already yields the class and the instance round-trip was redundant.
Returning it directly gives the same result for every populated bucket,
including duplicate class names, and no longer needs a live instance.

Ref mesa#3849
@github-actions github-actions Bot added the testing Release notes label label Sep 10, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -5.1% [-5.8%, -4.3%] 🔵 -0.4% [-0.6%, -0.1%]
BoltzmannWealth large 🔵 -1.4% [-2.0%, -0.8%] 🟢 -8.2% [-10.3%, -5.4%]
Schelling small 🔵 -1.2% [-2.3%, +0.1%] 🔵 +1.8% [+0.7%, +3.0%]
Schelling large 🔵 +2.5% [+1.7%, +3.3%] 🔴 +5.8% [+4.2%, +7.5%]
WolfSheep small 🔵 +1.2% [+0.7%, +1.7%] 🔵 +4.9% [+0.8%, +9.0%]
WolfSheep large 🔵 +0.2% [-0.3%, +0.6%] 🔵 +0.9% [-0.1%, +2.0%]
SugarscapeG1mt small 🔵 +0.4% [+0.1%, +0.7%] 🔵 +2.9% [+0.9%, +5.1%]
SugarscapeG1mt large 🔵 -0.6% [-1.5%, +0.4%] 🔵 +0.1% [-2.4%, +2.1%]
BoidFlockers small 🔵 -1.8% [-2.2%, -1.4%] 🔵 -0.1% [-0.6%, +0.3%]
BoidFlockers large 🔵 -2.3% [-2.6%, -2.0%] 🔵 +0.2% [-0.2%, +0.5%]
MultiLevelAllianceModel small 🔵 -1.6% [-2.8%, -0.5%] 🔵 -1.0% [-1.2%, -0.7%]
MultiLevelAllianceModel large 🔵 -1.2% [-1.7%, -0.6%] 🔵 -1.4% [-2.3%, -0.7%]

@tpike3

tpike3 commented Oct 19, 2026

Copy link
Copy Markdown
Member

@shipitdev Thank you for the contribution, as it is a draft and there has been some changes to meta-agent, I am going to close but please feel free to reopen once you complete if you still feel it is needed.

@tpike3 tpike3 closed this Oct 19, 2026
tpike3 added a commit that referenced this pull request Nov 10, 2026
… agent (#3970)

* Answer the class lookup from the dict key, not from a live agent

extract_class looked a class up by name and then read the class back off an
arbitrary instance:

    agent_type = agent_type_names.get(new_agent_class)
    if agent_type is None:
        return None
    return type(next(iter(agents_by_type[agent_type])))

agents_by_type is keyed by the agent class itself, and Model.register_agent
keys it by exact type(agent), so type(instance) is always the key the
instance was found under. The round trip answered nothing, but it did make
the result depend on the population: dissolving the last group of a name
leaves an empty bucket behind, so the name lookup still succeeded while
there was no instance left to take the class from, and next(iter(...))
raised StopIteration.

    team = membership.create("Team", [a, b])
    membership.dissolve(team)
    membership.create("Team", [a, b])      # StopIteration

Returning the key fixes the crash and makes the lookup independent of
whether any instance is alive, which is what the docstring promises.

The empty bucket is not the bug. #3809 added a regression test locking that
behaviour in, because WolfSheep indexes agents_by_type[Wolf] every step and
expects an empty set rather than a KeyError once that population dies out.
Pruning the bucket would break that, so the fix belongs here.

@shipitdev diagnosed this in #3850 and reached the same one-line change;
that PR was closed as a stale draft after the meta-agents rewrite, without
review of the approach.

Adds three tests: the reported dissolve-then-recreate path, an invariant
that the lookup gives the same class with and without live instances, and
the unknown-name case so the None early return stays covered. The first two
fail on main with StopIteration.

Fixes #3849

* Describe extract_class and its test generically in their docstrings

Both docstrings were written as an account of the bug this PR fixes.
Per PEP 257 they should describe what the function and the test do, so
rewrite them that way: extract_class gets a summary line plus Args and
Returns in the package's Google style, and the test's docstring states
the behaviour it asserts. No behaviour change.

Co-Authored-By: Claude Opus 5 <[email protected]>

---------

Co-authored-by: Claude Opus 5 <[email protected]>
Co-authored-by: Tom Pike <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants