mesa / mesa/mesa-llm

bug: move_one_step() moves agents in wrong direction on real OrthogonalMooreGrid

Open
#197 0 comments 0 reactions 0 assignees View on GitHub
depedency mesa-4
Dominant language
Python
Stars
73
Forks
89
Avg merge
14d 58m
Merged PRs (30d)
2

Description

## Describe the bug

`move_one_step()` in `inbuilt_tools.py` incorrectly uses the `connections`
dict on real `OrthogonalMooreGrid` cells to determine the target cell.

The problem is that real Mesa 4.x grid cells have a `connections` dict
populated with `(row, col)` delta keys internally, while the grid's `_cells`
dict uses `(x, y)` coordinates. When `move_one_step()` looks up
`connections[(-1, 0)]` for North, it gets the cell at `(row-1, col)` instead
of `(x, y+1)` — causing the agent to move in the wrong direction entirely.

## To Reproduce
```python
from mesa.model import Model
from mesa.agent import Agent
from mesa.discrete_space import OrthogonalMooreGrid
from mesa_llm.tools.inbuilt_tools import move_one_step

class DummyModel(Model):
def __init__(self):
super().__init__()

class DummyAgent(Agent):
def step(self): pass

model = DummyModel()
model.grid = OrthogonalMooreGrid((5, 5), torus=False)

agent = DummyAgent(model=model)
cell = model.grid._cells.get((2, 2))
cell.add_agent(agent)
agent.cell = cell
agent.pos = (2, 2)

move_one_step(agent, "North")
print(agent.pos) # prints (1, 2) — WRONG, expected (2, 3)
```

## Expected behavior

Agent at `(2, 2)` moving North should end up at `(2, 3)` — i.e. `y + 1` in
`(x, y)` convention which is what `OrthogonalMooreGrid._cells` uses.

## Actual behavior

Agent ends up at `(1, 2)` — the `connections` dict uses `(row, col)`
convention internally, so `connections[(-1, 0)]` (North) returns the cell
at `row-1` instead of `y+1`.

## Root cause

`OrthogonalMooreGrid` cells have a `connections` dict populated with
`(row, col)` delta keys. The previous code checked `connections` for all
cell types without distinguishing between real Mesa cells and
`SimpleNamespace` dummy cells used in tests. Both have a `connections`
attribute but with incompatible coordinate conventions.

## Fix

Check `isinstance(current_cell, SimpleNamespace)` to distinguish cell types:
- `SimpleNamespace` dummy cells → use `connections` with `(row, col)` deltas
- Real Mesa `Cell` objects → skip `connections`, use `direction_map_xy`
with `_cells` lookup directly

This fix is included in PR #195.

## Additional context

This bug affects all 8 cardinal and diagonal directions on real grids.
Torus wrapping is also broken as a consequence since the wrong starting
cell is used for the modulo calculation.

Also affects:
- Boundary detection (agent appears to hit wrong boundary)
- Occupied cell detection (wrong target cell is checked for capacity)

Contributor guide

Open the contributing guide

Research direction

Review PR #195 and start in mesa_llm/tools/inbuilt_tools.py at move_one_step(). Check the real OrthogonalMooreGrid reproduction from the issue, then verify that cardinal and diagonal movement, boundary and occupied-cell checks, and torus wrapping use the expected coordinates for completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.