Repository navigation
Answer the meta-agent class lookup from the dict key, not from a live agent - #3970
Merged
Merged
Conversation
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
|
Performance benchmarks:
|
tpike3
reviewed
Nov 2, 2026
| 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. |
Member
There was a problem hiding this comment.
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/
tpike3
reviewed
Nov 2, 2026
| 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. |
Member
There was a problem hiding this comment.
Similar comment as above, please right the docstring in relation to the function/test not the specific of the bug fix.
tpike3
requested changes
Nov 2, 2026
tpike3
left a comment
Member
There was a problem hiding this comment.
Thanks @lahari936 and @shipitdev for finding this bug.
Code is pretty simple change, please just update the 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]>
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
Use this template for bug fixes only.
Pre-PR Checklist
Summary
extract_classlooked a class up by name and then read the class back off an arbitrary live instance.agents_by_typeis 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:Dissolving the last group of a name leaves an empty bucket behind, so the name lookup still succeeds, the
Noneearly return does not fire, andnext(iter(...))runs on an empty set.Implementation
Model.register_agentkeysagents_by_typeby exacttype(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 aKeyErroronce 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 theStopIteration, 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:extract_classreturns the same class with and without live instances, asserting the bucket is empty but retained in betweenNoneearly return stays coveredThe first two fail on
mainwithStopIteration; the third passes either way by design, since it guards the path the fix keeps.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 checkandruff format --checkclean.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.