Skip to content

Keep model data consistent when a reporter fails - #3913

Merged
quaquel merged 5 commits into
mesa:mainfrom
SahilKumar75:fix/atomic-model-collection
Nov 21, 2026
Merged

quaquel merged 5 commits into
mesa:mainfrom
SahilKumar75:fix/atomic-model-collection

Conversation

@SahilKumar75

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Keep previously collected model data consistent when a model reporter raises an exception.

Bug / Issue

Fixes #3911.

DataCollector appends the collection timestamp before evaluating reporters, and appends each reporter value immediately. A failed reporter can leave a timestamp without a row or columns with different lengths. Reading the collected data can then fail even after the caller handles the original exception.

Implementation

Evaluate and copy all model reporter values into a temporary row first. Append those values and the collection timestamp only after all model reporters finish successfully. Reporter errors still propagate to the caller.

Testing

Added two regression cases covering a failure on the first collection and after a successful collection. Both check that existing rows and timestamps remain unchanged and that collection can recover after the reporter stops failing. Both failed before the fix.

All 49 tests in tests/test_datacollector.py pass with the fix on Python 3.12. Repository precommit checks pass for both changed files, including Ruff, formatting, pyupgrade, whitespace and codespell.

Additional Notes

This change protects stored model rows from reporter evaluation or copying failures. It does not roll back side effects inside user reporters and does not make model and agent collection a single transaction. No new dependencies or public API changes. The complete Mesa suite was not run.

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

Copy link
Copy Markdown
Contributor Author

The examples job fails while constructing VirusAntibodyModel: it passes a sequence of length 3 when creating 20 agents. The failure occurs before DataCollector is called. I reproduced the same ValueError through the agent argument resolver locally, and both agent.py and agentset.py are unchanged from the PR base. The 49 DataCollector tests pass locally. I am leaving the unrelated example code outside this fix.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.0% [-1.1%, +1.1%] 🔵 +0.2% [-0.2%, +0.6%]
BoltzmannWealth large 🔵 -1.6% [-2.2%, -1.1%] 🔵 -2.1% [-3.9%, -0.4%]
Schelling small 🔵 -0.1% [-0.7%, +0.5%] 🔵 +1.0% [-0.4%, +2.4%]
Schelling large 🔵 -0.9% [-1.5%, -0.2%] 🟢 -9.8% [-16.7%, -3.0%]
WolfSheep small 🟢 -7.4% [-8.3%, -6.2%] 🟢 -8.9% [-12.9%, -5.2%]
WolfSheep large 🔵 -2.1% [-3.4%, -1.0%] 🔵 -3.9% [-6.6%, -2.3%]
SugarscapeG1mt small 🔵 -1.2% [-1.9%, -0.6%] 🔵 -2.0% [-3.6%, -0.3%]
SugarscapeG1mt large 🔵 -5.1% [-7.3%, -2.9%] 🔵 -2.9% [-5.5%, -0.5%]
BoidFlockers small 🔵 -3.1% [-4.1%, -2.2%] 🔵 +0.0% [-0.5%, +0.5%]
BoidFlockers large 🔵 -2.0% [-2.5%, -1.6%] 🔵 -0.5% [-0.9%, +0.0%]
MultiLevelAllianceModel small 🔵 -4.8% [-7.8%, -1.5%] 🔵 +0.9% [-0.6%, +2.6%]
MultiLevelAllianceModel large 🟢 -6.6% [-8.6%, -4.4%] 🔵 -0.6% [-1.4%, +0.2%]

@dietcoke-17 dietcoke-17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. The fix is correct and minimal: evaluating and deep-copying into model_values, then committing the row and timestamp together, is the right shape. One test change before merge.

Requested change

  • collect_first=False never exercises the new path. On a first collect, _validate_model_reporter runs the reporter and wraps its error in RuntimeError before anything is appended, which is why the test has to accept (ValueError, RuntimeError). It only guards timestamp ordering, not the partial-row bug. Please use a reporter that passes validation but fails on evaluation (e.g. "second": [report, [model]]) and assert ValueError exactly.

Suggestions

  • Strengthen the recovery check: assert the new row's values and _collection_steps, not just len.
  • The description says copy failures are covered, but no test does. Add a __deepcopy__-raises case or drop the claim.
  • Expand the collect() docstring: a failing model reporter records nothing for that call, and agent data and reporter side effects aren't rolled back.

Design question (non-blocking)
Model rows now commit before agent collection runs. If an agent reporter fails, model_vars ends one row ahead of _agent_records, and a retry duplicates the model row. That's fine to scope out. But since #3916 does the same for agent types, would a single evaluate-all-then-commit pass be cleaner?

Nits

  • Set value per branch and deepcopy once, instead of four repeated model_values[var] = deepcopy(...).
  • Link mesa-examples#488 for the failing examples job, like #3916 does.

@SahilKumar75

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I pushed ce9d489f to address it:

  • The test now uses a list-style reporter, so validation succeeds and the reporter raises during collection. It checks ValueError on both the first and a later collect.
  • Recovery checks assert the stored row values and _collection_steps.
  • A separate test covers a value whose __deepcopy__ raises. I expanded the collect() docstring and consolidated the copy paths.

All 50 DataCollector tests and the changed file pre commit hooks pass locally. The examples failure is tracked in mesa/mesa-examples#488, with a proposed fix in #496.

A single evaluate then commit pass could give model and agent data a stronger whole call guarantee. I kept this PR scoped to model rows; I’m happy to discuss that broader contract separately.

@quaquel quaquel added the bug Release notes label label Oct 20, 2026
Comment thread mesa/datacollection.py
model_values[var] = deepcopy(value)

for var, value in model_values.items():
self.model_vars[var].append(value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this not

Suggested change
self.model_vars[var].append(value)
self.model_vars[var].append(deepcopy(value))

@quaquel quaquel added the backport-candidate PRs we might want to backport to an earlier branch label Nov 7, 2026
@quaquel
quaquel merged commit 7379e9e into mesa:main Nov 21, 2026
16 of 17 checks passed
EwoutH pushed a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
* Fix partial model data collection after reporter failures

Signed-off-by: Sahil Kumar Singh <[email protected]>

* test(datacollector): cover model reporter evaluation and copy failures

Signed-off-by: Sahil Kumar Singh <[email protected]>

---------

Signed-off-by: Sahil Kumar Singh <[email protected]>
Co-authored-by: Jan Kwakkel <[email protected]>

Adapted for 3.5.x: 3.5 records the collection step even without model reporters (mesa#3579), so the step is appended after the model reporter block, once all model reporters have succeeded, instead of inside it.

(cherry picked from commit 7379e9e)

Assisted-by: Claude Opus 5.5
EwoutH added a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
Add mesa#3913, mesa#4061, mesa#4072, mesa#4105, mesa#4126, mesa#4136, mesa#4141, mesa#4157, mesa#4161 and the Starlette 1.x support backported from mesa#4007, and move the release date to 2026-11-22.

Assisted-by: Claude Opus 5.5
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 model reporters leave DataCollector columns and timestamps inconsistent

3 participants