Repository navigation
Keep model data consistent when a reporter fails - #3913
Conversation
Signed-off-by: Sahil Kumar Singh <[email protected]>
|
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. |
|
Performance benchmarks:
|
dietcoke-17
left a comment
There was a problem hiding this comment.
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=Falsenever exercises the new path. On a first collect,_validate_model_reporterruns the reporter and wraps its error inRuntimeErrorbefore 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 assertValueErrorexactly.
Suggestions
- Strengthen the recovery check: assert the new row's values and
_collection_steps, not justlen. - 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
valueper branch anddeepcopyonce, instead of four repeatedmodel_values[var] = deepcopy(...). - Link mesa-examples#488 for the failing examples job, like #3916 does.
Signed-off-by: Sahil Kumar Singh <[email protected]>
|
Thanks for the detailed review. I pushed
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. |
| model_values[var] = deepcopy(value) | ||
|
|
||
| for var, value in model_values.items(): | ||
| self.model_vars[var].append(value) |
There was a problem hiding this comment.
why is this not
| self.model_vars[var].append(value) | |
| self.model_vars[var].append(deepcopy(value)) |
* 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
Pre-PR Checklist
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.