mesa icon indicating copy to clipboard operation
mesa copied to clipboard

discrete_space: Synchronize Cell-Agent References in Cell.add_agent and Cell.remove_agent

Open shipitdev opened this issue 3 months ago • 5 comments

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:

  1. cell.agents: The list of agents occupying the cell.
  2. 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) adds agent to cell._agents, but fails to update agent._mesa_cell or remove agent from its previous cell. The agent exists in multiple cells simultaneously!
  • cell.remove_agent(agent) removes agent from cell._agents, but fails to set agent._mesa_cell = None. The agent enters a "phantom state" where agent.cell points 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):

  1. add_agent(agent): If agent belongs to a different cell (old_cell), remove it from old_cell, append to self._agents, and set agent._mesa_cell = self.
  2. remove_agent(agent): Remove agent from self._agents, and if agent._mesa_cell is self, set agent._mesa_cell = None.

shipitdev avatar Aug 08 '26 18:08 shipitdev

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()?

ShashankSaga avatar Aug 10 '26 09:08 ShashankSaga

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.

shipitdev avatar Aug 10 '26 16:08 shipitdev

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.

ShashankSaga avatar Aug 10 '26 16:08 ShashankSaga

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.

quaquel avatar Aug 17 '26 07:08 quaquel

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.

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.

shipitdev avatar Aug 19 '26 07:08 shipitdev