Skip to content

Answer the meta-agent class lookup from the dict key, not from a live agent - #3970

Merged
tpike3 merged 3 commits into
mesa:mainfrom
lahari936:fix-extract-class-empty-bucket
Nov 10, 2026
Merged

tpike3 merged 3 commits into
mesa:mainfrom
lahari936:fix-extract-class-empty-bucket

Conversation

@lahari936

Copy link
Copy Markdown
Contributor

Note

Use this template for bug fixes only.

Pre-PR Checklist

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

Summary

extract_class looked a class up by name and then read the class back off an arbitrary live instance. agents_by_type is keyed by the class, so the key was already the answer — and the round trip made the result depend on whether any instance happened to be alive.

Bug / Issue

Fixes #3849. Still reproduces on current main:

team = membership.create("Team", [a, b])
membership.dissolve(team)
membership.create("Team", [a, b])      # StopIteration
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])))

Dissolving the last group of a name leaves an empty bucket behind, so the name lookup still succeeds, the None early return does not fire, and next(iter(...)) runs on an empty set.

Implementation

-    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 agents_by_type by exact type(agent), so for every instance in a bucket, type(instance) is the key it was found under. The round trip was a no-op that could fail. Returning the key fixes the crash and makes the lookup independent of the population, which is what the docstring promises.

The empty bucket is not the bug. #3809 added a regression test locking that behaviour in deliberately: WolfSheep indexes agents_by_type[Wolf] every step and expects an empty set rather than a KeyError once that population dies out. That PR's own description warns against "a well-meaning change (for instance, deleting the bucket to 'clean up')". So pruning the bucket, or catching the StopIteration, would each leave the actual defect in place — the fix belongs in the lookup.

Worth noting the fix also preserves what the function is for: recreating a dissolved name reuses the original class rather than building a fresh one.

Testing

Three tests in tests/test_meta_agents.py:

  • the reported dissolve-then-recreate path, asserting the rebuilt group reuses the original class
  • an invariant test that extract_class returns the same class with and without live instances, asserting the bucket is empty but retained in between
  • the unknown-name case, so the None early return stays covered

The first two fail on main with StopIteration; the third passes either way by design, since it guards the path the fix keeps.

pytest tests/test_meta_agents.py        13 passed
pytest tests/ -k meta                   62 passed
pytest tests/                           914 passed

The single full-suite failure is test_suppressed_wake_is_logged_at_debug, the pre-existing logging-propagation bug in #3935 (fixed by #3938 once it lands), unrelated to this change. ruff check and ruff format --check clean.

Additional Notes

@shipitdev diagnosed this in #3850 and reached the same one-line change. That PR was closed by @tpike3 as a stale draft after the meta-agents rewrite, without the approach itself being reviewed — credit for finding it is theirs, and I am happy to step aside if they would rather finish it.

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. mesa#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 mesa#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 mesa#3849
Copilot AI balanced review requested due to automatic review settings October 22, 2026 17:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Oct 22, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.3% [-0.6%, +0.0%] 🔵 -0.7% [-0.9%, -0.5%]
BoltzmannWealth large 🔵 +0.2% [-0.2%, +0.6%] 🔵 +1.2% [+0.9%, +1.5%]
Schelling small 🔵 +0.2% [-0.2%, +0.5%] 🔵 +0.3% [-0.4%, +0.9%]
Schelling large 🔵 -1.2% [-1.7%, -0.8%] 🔵 +0.6% [-0.3%, +1.4%]
WolfSheep small 🔵 +0.1% [-0.3%, +0.5%] 🔵 +1.5% [-2.3%, +5.8%]
WolfSheep large 🔵 -0.7% [-1.4%, +0.0%] 🔵 -0.9% [-1.3%, -0.5%]
SugarscapeG1mt small 🔵 +1.8% [+1.3%, +2.2%] 🔵 +0.8% [-0.9%, +2.6%]
SugarscapeG1mt large 🔵 +1.8% [+1.1%, +2.5%] 🔵 +2.7% [+1.4%, +4.1%]
BoidFlockers small 🔵 +0.3% [-0.0%, +0.7%] 🔵 -0.6% [-0.9%, -0.2%]
BoidFlockers large 🔵 +0.9% [+0.4%, +1.4%] 🔵 -2.0% [-2.2%, -1.8%]
MultiLevelAllianceModel small 🟢 -4.9% [-6.2%, -3.4%] 🔴 +3.9% [+3.4%, +4.4%]
MultiLevelAllianceModel large 🔵 -2.5% [-3.8%, -1.4%] 🔴 +5.4% [+5.1%, +5.7%]

answer. Reading the class back off a live instance instead made the result
depend on the population: a type whose agents have all been removed keeps
its (now empty) bucket, so the name lookup still succeeded while there was
no instance left to take the class from.

@tpike3 tpike3 Nov 2, 2026 •

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.

The docstring is written in the context of the bug fix, it needs to remain generic or be written generically with the purpose of the function. Here is the PEP guidance on docstrings - https://peps.python.org/pep-0257/

Comment thread tests/test_meta_agents.py Outdated
Dissolving the last group leaves an empty bucket in ``agents_by_type``
rather than removing the key, which is deliberate (see #3809). Looking the
class up by name therefore still succeeded while no instance remained to
read the class from, and ``create`` raised StopIteration. See #3849.

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.

Similar comment as above, please right the docstring in relation to the function/test not the specific of the bug fix.

@tpike3 tpike3 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.

Thanks @lahari936 and @shipitdev for finding this bug.

Code is pretty simple change, please just update the docstrings.

lahari936 and others added 2 commits November 2, 2026 16:51
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]>

@tpike3 tpike3 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! Thanks @lahari936!

@tpike3
tpike3 merged commit f6f7e53 into mesa:main Nov 10, 2026
14 of 16 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
… agent (#3970)

Adapted for 3.5.x: 3.5 has the pre-rewrite meta-agents API, so the same one-line fix is applied to its extract_class and a regression test is written for create_meta_agent (remove the last instance, then create the class again by name), which raised StopIteration before.

(cherry picked from commit f6f7e53)

Assisted-by: Claude Opus 5.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

extract_class raises StopIteration when a meta-agent group name is reused after dissolve

3 participants