Skip to content

Preserve agent type snapshots when reporters fail - #3916

Merged
quaquel merged 2 commits into
mesa:mainfrom
SahilKumar75:fix/atomic-agenttype-snapshots
Nov 7, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
SahilKumar75:fix/atomic-agenttype-snapshots

Conversation

@SahilKumar75

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Preserve a complete agent type snapshot if a reporter fails during collection. Previously, collecting again at the same model time cleared the stored snapshot before evaluating every reporter, so an exception could erase previously collected data or leave only some agent types.

Bug / Issue

Fixes #3915.

Implementation

Build the records for all configured agent types in a temporary dictionary, then replace the stored snapshot after every reporter succeeds. Successful collection keeps the existing replacement behavior. The original exception still propagates.

This change is limited to agent type snapshots. It does not make model and agent collection one transaction or undo side effects inside reporter functions. It is independent of #3913, which addresses model reporter rows.

Testing

Six regression cases fail on the base implementation and pass with this change. They cover failure in the first or second agent type during initial collection, replacement at the same time, and collection at a new time. They also check the original exception object and successful retry through the stored records and exported dataframe.

The full DataCollector test module passes: 53 tests. The agent, AgentSet and model modules also pass: 67 tests. Ruff check, Ruff format check and git diff check pass.

Commands use an isolated environment with this checkout installed:

uv run --no-project --python .venv/bin/python --with-editable . --with pytest --with pytest-cov python -m pytest -q tests/test_datacollector.py
uv run --no-project --python .venv/bin/python --with-editable . --with pytest --with pytest-cov python -m pytest -q tests/test_agent.py tests/test_agentset.py tests/test_model.py
uv tool run ruff check mesa/datacollection.py tests/test_datacollector.py
uv tool run ruff format --check mesa/datacollection.py tests/test_datacollector.py

Additional Notes

The full repository suite was not run. Local coverage attempts failed during NumPy import with cannot load module more than once per process; no coverage percentage is claimed. Normal pytest execution passes.

The separate examples job has a known VirusAntibodyModel failure tracked in mesa/mesa-examples#488, with a proposed fix in mesa/mesa-examples#496. This PR does not change that example or disable checks.

@github-actions github-actions Bot added the testing Release notes label label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.1% [-1.7%, -0.6%] 🔵 -0.5% [-0.8%, -0.2%]
BoltzmannWealth large 🔵 -0.4% [-0.9%, +0.1%] 🔵 -1.5% [-1.9%, -1.0%]
Schelling small 🔵 +0.1% [-0.4%, +0.5%] 🔵 -0.1% [-0.9%, +0.7%]
Schelling large 🔵 +0.6% [-0.0%, +1.3%] 🔵 +3.1% [+1.9%, +4.2%]
WolfSheep small 🔵 +0.4% [-0.0%, +0.9%] 🔵 -0.4% [-4.9%, +4.6%]
WolfSheep large 🔵 -1.2% [-1.7%, -0.6%] 🔵 -0.4% [-1.3%, +0.5%]
SugarscapeG1mt small 🔵 +0.0% [-0.4%, +0.5%] 🔵 -2.0% [-3.5%, -0.4%]
SugarscapeG1mt large 🔵 -1.3% [-1.8%, -0.6%] 🔵 -0.3% [-2.3%, +1.7%]
BoidFlockers small 🔵 -2.1% [-2.3%, -1.9%] 🔵 +0.9% [+0.5%, +1.4%]
BoidFlockers large 🔵 -0.9% [-1.5%, -0.4%] 🔵 +0.4% [+0.2%, +0.6%]
MultiLevelAllianceModel small 🔵 -0.4% [-1.4%, +0.6%] 🔵 +0.3% [+0.2%, +0.5%]
MultiLevelAllianceModel large 🔵 -1.2% [-2.4%, -0.3%] 🔵 +1.0% [+0.4%, +1.9%]

@SahilKumar75

Copy link
Copy Markdown
Contributor Author

The examples job fails in VirusAntibodyModel because a sequence of length 3 is passed when creating 20 agents. The failure is in agentset.py before DataCollector runs. The build matrix, coverage and documentation checks pass. The example correction is already being handled in mesa/mesa-examples#496, so I have kept it outside this PR.

@quaquel quaquel added bug Release notes label backport-candidate PRs we might want to backport to an earlier branch labels Nov 7, 2026
@quaquel
quaquel merged commit f3dc333 into mesa:main Nov 7, 2026
17 of 18 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Co-authored-by: Jan Kwakkel <[email protected]>
(cherry picked from commit f3dc333)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate PRs we might want to backport to an earlier branch bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed agent type collection replaces a complete snapshot with partial data

2 participants