From 302cef2d3fc84dcb6b86e419deb7bf65af987ff0 Mon Sep 17 00:00:00 2001 From: Alvi Rahman <46807496+Alvi-Rahman@users.noreply.github.com> Date: Wed, 4 Nov 2026 18:47:41 +0000 Subject: [PATCH] fix(controller): save the final checkpoint at the iteration reached When target_score or early stopping ended a run, the final checkpoint was named after the planned last iteration and stored it as last_iteration, so resuming from it skipped every iteration in between. If the planned last iteration was not a multiple of checkpoint_interval, no final checkpoint was written at all. --- openevolve/controller.py | 8 ++++- tests/test_checkpoint_resume.py | 59 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/openevolve/controller.py b/openevolve/controller.py index 92a1c35bd..08ff334b0 100644 --- a/openevolve/controller.py +++ b/openevolve/controller.py @@ -589,7 +589,13 @@ async def _run_evolution_with_checkpoints( # Note: start_iteration here is the evolution start (1 for fresh start, not 0) # max_iterations is the number of evolution iterations to run final_iteration = start_iteration + max_iterations - 1 - if final_iteration > 0 and final_iteration % self.config.checkpoint_interval == 0: + if self.database.last_iteration < final_iteration: + # The run ended before its planned last iteration (target score or + # early stopping). Save under the iteration actually reached, otherwise + # the checkpoint claims iterations that never ran and a resume skips them. + if self.database.last_iteration > 0: + self._save_checkpoint(self.database.last_iteration) + elif final_iteration > 0 and final_iteration % self.config.checkpoint_interval == 0: self._save_checkpoint(final_iteration) def _save_best_program(self, program: Optional[Program] = None) -> None: diff --git a/tests/test_checkpoint_resume.py b/tests/test_checkpoint_resume.py index 5d2e5662e..d91e63355 100644 --- a/tests/test_checkpoint_resume.py +++ b/tests/test_checkpoint_resume.py @@ -351,6 +351,65 @@ async def run_test(): # Run the async test asyncio.run(run_test()) + def _run_until(self, reached_iteration, iterations): + """Run with an evolution loop that stops after reached_iteration, return checkpoint names""" + + async def run_test(): + with patch("openevolve.controller.Evaluator") as mock_evaluator_class: + mock_evaluator_class.return_value = MockEvaluator() + + controller = OpenEvolve( + initial_program_path=self.test_program_path, + evaluation_file=self.evaluator_path, + config=self.config, + output_dir=self.test_dir, + ) + + async def run_evolution(*args, **kwargs): + child = Program( + id="child", + code='def test_function():\n return "child"\n', + language="python", + metrics={"score": 0.9, "combined_score": 0.95}, + ) + controller.database.add(child, iteration=reached_iteration) + + with patch( + "openevolve.controller.ProcessParallelController" + ) as mock_controller_class: + mock_controller = Mock() + mock_controller.run_evolution = AsyncMock(side_effect=run_evolution) + mock_controller.start = Mock(return_value=None) + mock_controller.stop = Mock(return_value=None) + mock_controller.shutdown_event = Mock() + mock_controller.shutdown_event.is_set.return_value = False + mock_controller.produced_no_candidates = False + mock_controller_class.return_value = mock_controller + + await controller.run(iterations=iterations) + + asyncio.run(run_test()) + return sorted(os.listdir(os.path.join(self.test_dir, "checkpoints"))) + + def test_final_checkpoint_after_early_stop_uses_reached_iteration(self): + """A run that stops early must not save a checkpoint for iterations that never ran""" + self.config.checkpoint_interval = 10 + + checkpoints = self._run_until(reached_iteration=37, iterations=100) + + self.assertEqual(checkpoints, ["checkpoint_37"]) + metadata_path = os.path.join(self.test_dir, "checkpoints", "checkpoint_37", "metadata.json") + with open(metadata_path) as f: + self.assertEqual(json.load(f)["last_iteration"], 37) + + def test_final_checkpoint_after_full_run(self): + """A run that reaches its last iteration still saves the final checkpoint""" + self.config.checkpoint_interval = 10 + + checkpoints = self._run_until(reached_iteration=100, iterations=100) + + self.assertEqual(checkpoints, ["checkpoint_100"]) + if __name__ == "__main__": unittest.main()