diff --git a/openevolve/controller.py b/openevolve/controller.py index 8c306f7b9..c80e9266a 100644 --- a/openevolve/controller.py +++ b/openevolve/controller.py @@ -384,6 +384,7 @@ async def run( Best program found Raises: + FileNotFoundError: If checkpoint_path is given but does not exist EvolutionFailedError: If iterations ran but none produced a candidate program """ # Ensure run_id exists even if instance was created without __init__ (e.g. in tests) @@ -414,7 +415,7 @@ async def _run_evolution( max_iterations = self.config.max_iterations if iterations is None else iterations # Determine starting iteration start_iteration = 0 - if checkpoint_path and os.path.exists(checkpoint_path): + if checkpoint_path: self._load_checkpoint(checkpoint_path) start_iteration = self.database.last_iteration + 1 logger.info(f"Resuming from checkpoint at iteration {start_iteration}") diff --git a/tests/test_checkpoint_resume.py b/tests/test_checkpoint_resume.py index 5d2e5662e..f02f13555 100644 --- a/tests/test_checkpoint_resume.py +++ b/tests/test_checkpoint_resume.py @@ -351,6 +351,38 @@ async def run_test(): # Run the async test asyncio.run(run_test()) + def test_missing_checkpoint_path_raises(self): + """A checkpoint_path that does not exist must not silently start a fresh run""" + + async def run_test(): + with patch("openevolve.controller.Evaluator") as mock_evaluator_class: + mock_evaluator = MockEvaluator() + mock_evaluator_class.return_value = mock_evaluator + + controller = OpenEvolve( + initial_program_path=self.test_program_path, + evaluation_file=self.evaluator_path, + config=self.config, + output_dir=self.test_dir, + ) + + missing = os.path.join(self.test_dir, "checkpoints", "checkpoint_50") + with patch( + "openevolve.controller.ProcessParallelController" + ) as mock_parallel_class: + mock_parallel = mock_parallel_class.return_value + mock_parallel.run_evolution = AsyncMock(return_value=None) + mock_parallel.shutdown_event.is_set.return_value = False + mock_parallel.produced_no_candidates = False + with self.assertRaisesRegex(FileNotFoundError, "checkpoint_50"): + await controller.run(iterations=0, checkpoint_path=missing) + + mock_parallel_class.assert_not_called() + self.assertEqual(len(controller.database.programs), 0) + self.assertEqual(mock_evaluator.call_count, 0) + + asyncio.run(run_test()) + if __name__ == "__main__": unittest.main()