Skip to content

Fix duplicate table dataset rows in recorder - #4164

Merged
quaquel merged 6 commits into
mesa:mainfrom
quaquel:fix/table-dataset-rows
Nov 25, 2026
Merged

quaquel merged 6 commits into
mesa:mainfrom
quaquel:fix/table-dataset-rows

Conversation

@quaquel

@quaquel quaquel commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

This PR fixes a bug when recording TableDataSet. A TableDataSet uses a list to store its rows. So, when taking a snapshot, it is mutable. Whenever, new rows are added, they show up in earlier snapshots. In addition, every snapshot handled the entire dataset, including rows seen before.

This PR solves both problems in one go by adding a cursor to the recorder that tracks which rows have been seen in previous snapshots. This approach is adequate, because TableDataSet can only ever be expanded by adding new rows to it. Rows cannot be removed.

Closes #3967

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Nov 21, 2026
@quaquel quaquel changed the title Fix/table dataset rows Fix duplicate table dataset rows in recorder Nov 21, 2026
@quaquel quaquel added the bug Release notes label label Nov 21, 2026
TableDataSet.data returns its live, append-only rows list. The recorders
stored it by reference on every collection, so all stored blocks aliased
the same list, and even with a copy each collection re-recorded the full
table (1+2+...+n rows with wrong timestamps).

BaseDataRecorder now takes a snapshot via _take_snapshot(): for a
TableDataSet it returns a new list holding only the rows added since the
previous collection (re-including the replaced block's rows on an
overwrite). Row dicts are shared with the dataset rather than copied,
since neither the dataset nor the recorders mutate them; the
TableDataSet.data docstring documents this. Other datasets are unchanged.

Adds a regression test across DataRecorder, JSONDataRecorder,
ParquetDataRecorder and SQLDataRecorder.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
Comment thread tests/experimental/test_datarecorder.py Resolved
Comment thread tests/experimental/test_datarecorder.py Resolved
@quaquel

quaquel commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Thanks for the detailed feedback. I am starting to wonder whether going down the cursor route is the right solution. At the moment, the main point of collect() is to take the state of a DataSet, time stamp it, and make it persistent. For TableDataSet, we have the persistence already: the list of rows in the dataset. All that is missing is the time stamp. What about automagically time stamping rows when they are added (i.e., via TableDataSet.add_row). Then all that the various recorders that do flush to disk need to do is check which rows are new and just flash those. It does entail a subtle behavior change: the timestamp shifts from when the row was collected to when the row was added. @codebreaker32, curious to get your perspective.

Regardless, I believe we should merge #4168 first.

quaquel and others added 3 commits November 22, 2026 14:40
- Add TableDataSet.get_rows_since(index), so BaseDataRecorder only
  tracks the cursor and no longer imports TableDataSet. The recorder
  looks the method up on the dataset's type, so only dataset classes
  that define it qualify.
- Document that window_size keeps the rows of the last N collections
  for a TableDataSet.
- Add a test that adds rows on only some steps and runs twice.
- Drop the dropna in the table test now that mesa#4168 is merged.
- Import TableDataSet at the top of the test module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
@codebreaker32

codebreaker32 commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

I am starting to wonder whether going down the cursor route is the right solution

I'd still favour the cursor, though I see the appeal. Recorders that write to disk would still need to track which rows they've already written, so stamping in add_row adds a second mechanism instead of replacing the cursor, and TableDataSet would need the model's clock.

It would also clash with the time column the recorders add. They build rows as {**row, "time": time}, so a row's own time gets replaced by the collection time: with interval=5, rows with time 1 to 5 all show 5(which is prevalent on current main too). That probably deserves its own issue. If you need the time a row was added, we can store it in a field like added_at.

@codebreaker32

Copy link
Copy Markdown
Collaborator

I have opened a separate issue for that bug #4185

@quaquel

quaquel commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

I'd still favour the cursor, though I see the appeal. Recorders that write to disk would still need to track which rows they've already written, so stamping in add_row adds a second mechanism instead of replacing the cursor, and TableDataSet would need the model's clock.

Fair point and it indeed points towards using a cursor.

quaquel and others added 2 commits November 22, 2026 20:21
Collecting again at an already recorded time (for example, the end of a
run followed by collect()) made the next recording of that time an
overwrite. The backends delete what is stored for that time on an
overwrite, but the cursor only re-supplied rows from the latest
collection, so rows were lost (e.g., run_for(2), collect(), run_for(1)
dropped the t=2 row in DataRecorder, ParquetDataRecorder and
SQLDataRecorder).

Rows of an append-only dataset never change once added, so an overwrite
has nothing to correct. The recorder now skips overwrites for such
datasets and only records rows it has not recorded yet, tracked with a
single forward-only counter per dataset. _is_append_only() holds the
class-level get_rows_since check.

Adds a regression test for the case above across all four recorders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
The time-change, run-end and manual collect() paths each decided
whether to overwrite, took a snapshot and stored it. The rule that
append-only datasets are never overwritten lived only in the first two;
collect() did not need it because it never overwrites.

Move these steps into _record_dataset(), used by all three paths. This
changes no behavior today, but keeps the rule in one place: once
collect() overwrites at an already recorded time (as mesa#4148 proposes),
a table's recorded rows would otherwise be deleted at that time while
the snapshot only holds the new rows.

Adds a test that an overwrite never deletes the recorded rows of an
append-only dataset, across all four recorders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01RbrUQUQAArrpH7rYQFt6KW
@quaquel

quaquel commented Nov 22, 2026

Copy link
Copy Markdown
Member Author

Thanks for the careful review, @codebreaker32. I've pushed changes covering all six points:

1. Rows lost when collecting again at an already recorded time (c61cee2, 3e055bf)

The root cause is that the backends delete whatever is stored for a specific time stamp on an overwrite, while the cursor only re-supplied the rows from the latest collection. So in your example (run_for(2), collect(), run_for(1)), the t=2 row was deleted and never stored again.

Rows of a TableDataSet never change once added, so an overwrite never has anything to correct. The recorder now never overwrites an append-only dataset. It records only the rows it hasn't recorded yet, tracked with a single counter per dataset that only moves forward. This also removes the block bookkeeping from the cursor, further simplifying the fix.

All three collection paths (time change, end of run, and explicit calls to collect()) now record through the same method, _record_dataset(), so the rule exists in one place. This matters also for #4148: once collect() overwrites at an already recorded time, it would otherwise delete a table's rows at that time.

I added some additional tests including your example and a direct check that an overwrite never deletes recorded table rows. These tests are run across all four recorders.

2. Empty snapshots: fixed by #4168, now merged into this branch. I added the test you suggested: rows on only some steps, run twice.

3. dropna: removed, now that #4168 is in.

4. window_size: documented that for a TableDataSet it keeps the rows of the last N collections. While checking this, I found that windowing on tables is inconsistent across backends:

  • With sparse rows, DataRecorder evicts on empty collections without storing anything, while JSON stores empty collections as entries, so the two keep different rows.
  • ParquetDataRecorder and SQLDataRecorder don't implement window_size at all.

The inconsistency of how window_size is handled across the four backends predates this PR, so I'd rather handle it in a follow-up PR.

5. Coupling: TableDataSet now has get_rows_since(index), and BaseDataRecorder no longer imports it. Instead, I rely on duck typing by checking if dataset is append-only by looking up get_rows_since(index) on the dataset's class. I don't do it on the instance, because that messes up the current mocking.

6. Nit: TableDataSet is imported at the top of the test module.

@codebreaker32 codebreaker32 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, everything looks good!

@EwoutH EwoutH left a comment

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.

Thanks, looks good. Tests pass locally for all four recorders.

@quaquel
quaquel merged commit a5d37f6 into mesa:main Nov 25, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataRecorder duplicates TableDataSet rows and gives them the wrong time

3 participants