Skip to content

Fix inconsistent state when assigning CellAgent to full cell#3374

Merged
quaquel merged 3 commits intomesa:mainfrom
souro26:fix-cell-assignment
Feb 24, 2026
Merged

Fix inconsistent state when assigning CellAgent to full cell#3374
quaquel merged 3 commits intomesa:mainfrom
souro26:fix-cell-assignment

Conversation

@souro26
Copy link
Copy Markdown
Contributor

@souro26 souro26 commented Feb 24, 2026

Summary

Prevents inconsistent state when assigning a CellAgent to a cell that is already at capacity. Previously, a failed assignment could leave the agent pointing to a cell that did not actually contain it.

Fixes #3373

Implementation

The setter logic for CellAgent.cell was reordered to ensure state is only mutated after successful placement.

The new behavior attempts to add the agent to the new cell first. Only if that succeeds, the agent from the old cell (if any) is removed and the agent's internal _cell reference is updated.

Testing

Added a minimal reproduction case that fills a cell, attempts to assign another agent and verifies whether an exception is raised., if agent.cell remains unchanged and if the agent is not present in cell.agents.

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.3% [-1.6%, -0.9%] 🔵 -2.7% [-2.8%, -2.5%]
BoltzmannWealth large 🔵 -0.9% [-1.3%, -0.5%] 🔵 -1.1% [-1.8%, -0.3%]
Schelling small 🔵 +1.0% [+0.8%, +1.3%] 🔵 +0.9% [+0.7%, +1.1%]
Schelling large 🔵 +0.6% [+0.2%, +1.1%] 🔵 +2.1% [+0.2%, +3.8%]
WolfSheep small 🟢 -7.3% [-7.5%, -7.0%] 🟢 -3.8% [-3.9%, -3.6%]
WolfSheep large 🟢 -7.4% [-7.7%, -7.1%] 🟢 -4.3% [-4.7%, -3.7%]
BoidFlockers small 🔵 -0.9% [-1.1%, -0.7%] 🔵 -1.1% [-1.3%, -0.9%]
BoidFlockers large 🔵 +0.0% [-0.3%, +0.3%] 🔵 +1.1% [+1.0%, +1.2%]

@quaquel quaquel merged commit 71b2b34 into mesa:main Feb 24, 2026
12 of 14 checks passed
@EwoutH EwoutH added the bug Release notes label label Mar 13, 2026
EwoutH pushed a commit that referenced this pull request Mar 15, 2026
* Fix inconsistent state when assigning CellAgent to full cell

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Jan Kwakkel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent assignment to a full cell leaves inconsistent state

3 participants