discrete_space: Synchronize Cell-Agent References in Cell.add_agent and Cell.remove_agent
PR Summary
discrete_space: Synchronize Cell-Agent References in Cell.add_agent and Cell.remove_agent
Problem Statement
In Mesa 3.0's discrete_space module, spatial occupation is represented by a bidirectional relationship between Cell and CellAgent:
cell.agents: The list of agents occupying the cell.agent.cell: The reference pointing to the agent's current cell.
When model builders use agent.move_to(cell) or agent.cell = cell, both references are updated consistently.
The Bug / State Corruption
When model builders call cell.add_agent(agent) or cell.remove_agent(agent) directly:
cell.add_agent(agent)addsagenttocell._agents, but fails to updateagent._mesa_cellor removeagentfrom its previous cell. The agent exists in multiple cells simultaneously!cell.remove_agent(agent)removesagentfromcell._agents, but fails to setagent._mesa_cell = None. The agent enters a "phantom state" whereagent.cellpoints to a cell that has no record of it.
Later in the simulation, when the phantom agent attempts to move (agent.move_to(new_cell) or agent.move("north")), Mesa calls old_cell.remove_agent(self). Since old_cell no longer contains the agent, Mesa crashes with AgentMissingException: Agent 1 is not in cell (0, 0).
Root Cause Analysis
In mesa/discrete_space/cell.py:
def add_agent(self, agent: CellAgent) -> None:
...
self._agents.append(agent) # Only mutates local list
def remove_agent(self, agent: CellAgent) -> None:
...
self._agents.remove(agent) # Only mutates local list
Cell.add_agent() and Cell.remove_agent() perform unidirectional mutation on self._agents without synchronizing agent._mesa_cell. This breaks the fundamental spatial invariant:
$$\text{agent} \in \text{cell.agents} \iff \text{agent.cell} == \text{cell}$$
Proposed Fix
Strategy: Atomic Two-Way Synchronization
Update Cell.add_agent() and Cell.remove_agent() so that mutating a cell's agent list automatically synchronizes the agent's internal cell reference (agent._mesa_cell):
add_agent(agent): Ifagentbelongs to a different cell (old_cell), remove it fromold_cell, append toself._agents, and setagent._mesa_cell = self.remove_agent(agent): Removeagentfromself._agents, and ifagent._mesa_cell is self, setagent._mesa_cell = None.
Hi! I investigated this issue and reproduced the inconsistent state locally. In particular, calling cell.add_agent(agent) adds the agent to cell.agents, but agent.cell remains None; adding the same agent to another cell can then leave the agent registered in both cells.
I also checked the existing discrete_space tests and found that they verify cell membership but don't currently cover the bidirectional Cell ↔ CellAgent invariant.
I’d like to work on this issue by adding regression tests for these cases and implementing the synchronization described here. Does this approach match the intended behavior for Cell.add_agent() and Cell.remove_agent()?
Hi! I investigated this issue and reproduced the inconsistent state locally. In particular, calling cell.add_agent(agent) adds the agent to cell.agents, but agent.cell remains None; adding the same agent to another cell can then leave the agent registered in both cells.
hi, have you tried to reproduce it in Mesa 4.0 architecture ?
I also checked the existing discrete_space tests and found that they verify cell membership but don't currently cover the bidirectional Cell ↔ CellAgent invariant.
I’d like to work on this issue by adding regression tests for these cases and implementing the synchronization described here. Does this approach match the intended behavior for Cell.add_agent() and Cell.remove_agent()?
i was actually keeping this issue to get triaged first but since its just that the dev forgot to reference or update the bidirectional design so i think you can raise a PR about it for review.
Yes, I reproduced it against the current Mesa main branch, which is reporting version 4.0.0a0. The reproduction showed that Cell.add_agent() updates cell.agents without updating agent.cell, and adding the same agent to another cell leaves it registered in both cells.
Thanks for confirming. I’ll proceed with the regression tests and the synchronization fix and open a PR for review.
I am not sure about this claimed bug. If you look at HasCell, the idea is that all agent cell updating runs through assigning to agent.cell. If this is done correctly, there is no bug as far as I can see.
I am not sure about this claimed bug. If you look at
HasCell, the idea is that all agent cell updating runs through assigning toagent.cell. If this is done correctly, there is no bug as far as I can see.
Hi @quaquel ,
Thanks for explaining! i think yes makes total sense. I see now that agent.cell = new_cell is meant to be the only way to move agents, and that it already takes care of adding and removing them automatically.I was manually calling add_agent() / remove_agent() in my setup, which caused the conflict here.
Since that's the intended behaviour/rule across Mesa, I completely agree on "this is no bug" . Thanks for clarifying, i'll go ahead and close this here.