Repository navigation
Conversation
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
|
Performance benchmarks:
|
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. |
1 task done
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]>
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.
Ref #3849.
Problem
Create a meta-agent group, dissolve it, create another with the same name →
StopIteration.extract_classlooks the class up by name, then reads the type back off a live instance. Dissolving the last group of a name empties thatagents_by_typebucket but keeps the key, so the name lookup still succeeds, theNoneearly-return doesn't fire, andnext(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.