Skip to content

fix: respect DataRecorder start_time when a run ends - #4041

Merged
quaquel merged 2 commits into
mesa:mainfrom
atul-upadhyay-7:fix-recorder-start-time
Nov 6, 2026
Merged

quaquel merged 2 commits into
mesa:mainfrom
atul-upadhyay-7:fix-recorder-start-time

Conversation

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

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

Summary

Skip the final recorder snapshot when a run ends before start_time.

Bug / Issue

Fixes #4038. The normal collection path waits for start_time, but RUN_ENDED could still record an early row.

Implementation

Add the missing start_time check in _on_run_ended. Final snapshots at or after start_time still work, including between collection intervals.

Testing

  • Added 26 cases covering all four backends, run_for/run_until, the start_time boundary and resuming a run.
  • Recorder tests: 83 passed in each of six fresh runs.
  • Full suite with coverage: 1,054 passed, 10 warnings.
  • Ruff check, format check and git diff --check passed.

Additional Notes

AI assistance was used to prepare the change and run the tests.

@quaquel ready for review

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -6.5% [-7.3%, -5.7%] 🔵 +1.1% [+0.2%, +2.1%]
BoltzmannWealth large 🟢 -5.6% [-6.5%, -4.6%] 🟢 -14.2% [-16.4%, -11.5%]
Schelling small 🟢 -7.2% [-8.1%, -6.3%] 🔵 -1.0% [-1.8%, -0.3%]
Schelling large 🟢 -4.5% [-4.9%, -4.1%] 🟢 -14.9% [-16.6%, -13.4%]
WolfSheep small 🟢 -8.3% [-9.8%, -6.6%] 🔵 -0.5% [-4.2%, +3.2%]
WolfSheep large 🟢 -10.2% [-11.2%, -9.1%] 🟢 -19.4% [-21.5%, -17.1%]
SugarscapeG1mt small 🟢 -6.6% [-8.2%, -5.0%] 🔵 -3.8% [-5.7%, -2.0%]
SugarscapeG1mt large 🟢 -8.9% [-12.2%, -5.2%] 🔵 -3.5% [-5.5%, -1.6%]
BoidFlockers small 🟢 -7.1% [-8.0%, -6.1%] 🔵 -1.1% [-1.6%, -0.5%]
BoidFlockers large 🟢 -5.7% [-7.0%, -4.2%] 🔵 -1.4% [-2.2%, -0.2%]
MultiLevelAllianceModel small 🟢 -16.1% [-17.8%, -14.0%] 🔵 -3.9% [-5.1%, -2.5%]
MultiLevelAllianceModel large 🔵 -1.0% [-4.2%, +2.1%] 🔵 -3.7% [-8.4%, +0.3%]

@AshNicolus AshNicolus 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.

Checked this out and ran it against main. The issue repro goes from [2.0] to [], runs ending at or after start_time are unchanged, and 10 of the new cases fail on main while the controls pass. Full suite passes here too. LGTM.

One thing I noticed while testing, not caused by this PR: the end side of the window behaves differently. With interval=5, end_time=20, a run ending at 12 records a final row at 12. With end_time=13 it doesn't, even though 12 is inside the window, because update_next_collection disables the dataset at time 10 once the next collection (15) would pass end_time. Not sure if that is intended. Happy to open a separate issue if it isn't.

@atul-upadhyay-7

Copy link
Copy Markdown
Contributor Author

thanks for testing this against main and for the review. i'll look into the end_time case and open a separate issue for it.

@quaquel
quaquel merged commit 1dde24a into mesa:main Nov 6, 2026
17 checks passed
EwoutH pushed a commit to EwoutH/mesa that referenced this pull request Nov 22, 2026
DatasetConfig.window_size is documented as the maximum number of snapshots to keep. DataRecorder applies it, but JSONDataRecorder kept every snapshot, so memory grew without bound and save_to_json wrote the full history.

Adapted for 3.5.x: 3.5 recorders do not record when a run ends (mesa#4041 is 4.0-only), so after run_for(4) the window holds times 2.0 and 3.0; DataRecorder gives the same. The test's expected values are changed accordingly.

(cherry picked from commit 668c892)

Assisted-by: Claude Opus 5.5
EwoutH pushed a commit that referenced this pull request Nov 26, 2026
- DataRecorder: an overwrite only replaces the last block if it holds
  the same time. When a run ended with an empty agent snapshot, which
  stores no block, the next run's overwrite removed the previous time's
  data instead.
- JSONDataRecorder: a snapshot taken without agents no longer adds a row
  with value=[] and NaN ids.
- JSON, Parquet and SQL recorders return an empty frame with a time
  column when nothing has been recorded, as DataRecorder does. The
  scenario runner rejects a frame without columns as a failed outcome.

Part of #4165.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Jan Kwakkel <[email protected]>

Adapted for 3.5.x: 3.5 recorders do not record when a run ends (#4041 is 4.0-only), so an overwrite of the last block never happens and the DataRecorder part is dropped, with its test and the test_run_ended test that main has next to it. The JSONDataRecorder empty-snapshot fix and the empty frame with a time column for the JSON, Parquet and SQL recorders are taken unchanged.

(cherry picked from commit be399e6)

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

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataRecorder records a final snapshot before start_time

3 participants