Skip to content

Add regression test for retained empty agents_by_type bucket - #3809

Merged
quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:test-agents-by-type-empty-bucket
Aug 26, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
AshNicolus:test-agents-by-type-empty-bucket

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

When the last agent of a given type is removed, its per-type AgentSet is kept
in agents_by_type as an empty set, and the type stays listed in agent_types.
This behavior was not covered by any test: test_agent_remove only checks that
model.agents is empty after remove_all_agents, and nothing asserted what happens
to the per-type buckets once a type goes extinct.

The behavior is relied on in practice. WolfSheep, for example, indexes
agents_by_type[Wolf] and agents_by_type[Sheep] every step and expects an empty
set rather than a KeyError once one of those populations dies out. Locking it
down with a test prevents a well-meaning change (for instance, deleting the
bucket to "clean up") from silently breaking those models.

The new test creates two agent types, removes all agents of one type, and
asserts the type is still in agent_types, that agents_by_type returns an empty
set for it, and that the other type's set is untouched. No production code is
changed.

Verified locally: full test suite passes (712 tests, examples and visualization
components included), ruff check and ruff format both clean.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.4% [-3.2%, +0.6%] 🔵 -0.8% [-1.2%, -0.3%]
BoltzmannWealth large 🔵 -0.9% [-3.6%, +1.6%] 🔵 -1.5% [-3.7%, +0.5%]
Schelling small 🔵 -2.7% [-4.1%, -1.2%] 🔵 -0.9% [-2.2%, +0.5%]
Schelling large 🔵 -2.8% [-3.6%, -2.1%] 🟢 -5.0% [-5.9%, -4.1%]
WolfSheep small 🔵 -3.8% [-5.5%, -1.9%] 🔵 -4.4% [-8.3%, -0.4%]
WolfSheep large 🔵 -0.1% [-1.6%, +1.9%] 🔵 +1.0% [-0.8%, +3.3%]
SugarscapeG1mt small 🔵 +0.2% [-1.5%, +1.7%] 🔵 -2.6% [-4.3%, -0.9%]
SugarscapeG1mt large 🔵 +1.4% [-1.2%, +3.9%] 🔵 +0.4% [-2.0%, +3.3%]
BoidFlockers small 🔵 +4.4% [+1.7%, +6.9%] 🔵 +0.3% [-0.7%, +1.5%]
BoidFlockers large 🔵 -0.8% [-4.3%, +2.5%] 🔵 -0.2% [-0.5%, +0.0%]

@dietcoke-17 dietcoke-17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM
One things that looked good to me is that :--

  1. list(model.agents_by_type[Prey]) before removing avoids mutating the
    set mid-iteration — mirrors the pattern Model.remove_all_agents()
    already uses internally.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Yes, the copy-before-iterate is deliberate so the test
mirrors how remove_all_agents handles the same mutate-during-iteration issue.

@quaquel quaquel added the testing Release notes label label Aug 26, 2026
@quaquel
quaquel merged commit 19eff50 into mesa:main Aug 26, 2026
12 checks passed
EwoutH pushed a commit that referenced this pull request Oct 28, 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.

3 participants