Skip to content

bug: SimulationRecorder.save() appends duplicate simulation_end events on repeated calls #201

@BhoomiAgrawal12

Description

@BhoomiAgrawal12

Describe the bug
SimulationRecorder.save() unconditionally appends a simulation_end event every time it is called. When a user calls save_recording() manually and then the atexit auto-save handler fires at program exit, the output file contains two simulation_end entries.

Expected behavior
simulation_end is recorded exactly once, regardless of how many times save() is called.

To Reproduce

import tempfile
from unittest.mock import MagicMock
from mesa_llm.recording.simulation_recorder import SimulationRecorder

model = MagicMock()
model.steps = 5
model.agents = []
model.__class__.__name__ = "TestModel"
model.max_steps = 10

with tempfile.TemporaryDirectory() as tmp:
    recorder = SimulationRecorder(model=model, output_dir=tmp)
    recorder.save("first_save.json")
    recorder.save("second_save.json")

    ends = [e for e in recorder.events if e.event_type == "simulation_end"]
    print(len(ends))  # this prints 2 where 1 is expected

Additional context
currently, each call to save() appends a new simulation_end event.

I will be raising a pr fixing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions